How to run java jar file in .net application
Hello to every one
I create one application in java technology.It works fine.But problem is, to run the (java application) jar file in .net application.I tried, but i didn't get the solution.Is there anybody know the way please help me, I eagerly waiting for your quick reply...........
Answers
- The following is the C# program to execute a program and get the return code. (This example launches NOTEPAD in "C:\Windows" directory.)
I believe you can convert the program into J# language.
using System;
using System.Text;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public UInt32 dwProcessId;
public UInt32 dwThreadId;
}
const UInt32 WAIT_OBJECT_0 = 0x00000000;
const UInt32 INFINITE = 0xFFFFFFFF;
[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[DllImport("kernel32.dll", EntryPoint = "CreateProcess", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
static extern bool CreateProcess(string appName, StringBuilder cmdLine, IntPtr processAttr, IntPtr threadAttr, bool inheritHandles, int creationFlag, IntPtr environment, IntPtr curDir, ref STARTUPINFO startupInfo, ref PROCESS_INFORMATION processInfo);
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "GetExitCodeProcess", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool GetExitCodeProcess(IntPtr hHandle, out Int32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool CloseHandle(IntPtr hHandle);
unsafe static void Main(string[] args)
{
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
if (CreateProcess(@"C:\WINDOWS\notepad.exe", null /* parameter */, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, ref si, ref pi))
{
uint result = WaitForSingleObject(pi.hProcess, INFINITE);
if (result == WAIT_OBJECT_0)
{
Int32 returnCode;
if (GetExitCodeProcess(pi.hProcess, out returnCode))
Console.Out.WriteLine(returnCode);
}
else
{
// error handler
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
All Replies
- You can use the process class. Change the example below to use the proper paths.
System.Diagnostics.Process.Start("java -jar myprog.jar"); How do you pass parameters and receive back results?
r
Ziad Abdel.Raheim Elmalki wrote: You can use the process class. Change the example below to use the proper paths.
System.Diagnostics.Process.Start("java -jar myprog.jar");well, this will actually lunch a new proccess. the application will not actually run in the context of the .NET Framework, but it will be run under the JVM.
the answer to your question is: you can't run a java application in the context of the .NET Framework.
rwielage wrote: How do you pass parameters and receive back results?
r
System.Diagnostics.Process.Start("java -jar myprog.jar list_of_parameters");
you will not be able to get the results from that process unless you use interop services.
- The following is the C# program to execute a program and get the return code. (This example launches NOTEPAD in "C:\Windows" directory.)
I believe you can convert the program into J# language.
using System;
using System.Text;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public UInt32 dwProcessId;
public UInt32 dwThreadId;
}
const UInt32 WAIT_OBJECT_0 = 0x00000000;
const UInt32 INFINITE = 0xFFFFFFFF;
[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[DllImport("kernel32.dll", EntryPoint = "CreateProcess", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
static extern bool CreateProcess(string appName, StringBuilder cmdLine, IntPtr processAttr, IntPtr threadAttr, bool inheritHandles, int creationFlag, IntPtr environment, IntPtr curDir, ref STARTUPINFO startupInfo, ref PROCESS_INFORMATION processInfo);
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "GetExitCodeProcess", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool GetExitCodeProcess(IntPtr hHandle, out Int32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool CloseHandle(IntPtr hHandle);
unsafe static void Main(string[] args)
{
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
if (CreateProcess(@"C:\WINDOWS\notepad.exe", null /* parameter */, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, ref si, ref pi))
{
uint result = WaitForSingleObject(pi.hProcess, INFINITE);
if (result == WAIT_OBJECT_0)
{
Int32 returnCode;
if (GetExitCodeProcess(pi.hProcess, out returnCode))
Console.Out.WriteLine(returnCode);
}
else
{
// error handler
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
step 1 create process object.
step 2 give EXE as file name which will going to run jar file i.e. JAVA
step 3 pas argument need for run
Eg.
Dim prc As New System.Diagnostics.Processprc.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Hiddenprc.StartInfo.FileName = Server.MapPath("~/Java/jdk/jre/bin/java.exe")prc.StartInfo.Arguments = " -jar " & """" & strPath & """"prc.Start()prc.WaitForExit()- Proposed As Answer bySairam2525 Thursday, September 17, 2009 5:46 AM
Hi
Could anyone let me know what is wrong with the below code.
It is not displaying any message.I have a button click event which runs .jar file from C#.NET application.
//Button Click Event for running .jar file
protected void btnRunJarApplication_Click(object sender, EventArgs e)
{
String strJarFilePath = "\\testHelloWorldNew.jar";Process proc = new Process();
try
{
proc.StartInfo.FileName = "java";
proc.StartInfo.Arguments = "-jar " + strJarFilePath;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.UseShellExecute = false;DirectoryInfo currentDir = new DirectoryInfo(Environment.CurrentDirectory);
proc.StartInfo.WorkingDirectory = currentDir.FullName;proc.Start();
//give Processor 5sec to close
proc.WaitForExit(5000);
proc.ExitCode;
}
catch (Exception ex)
{
if (!proc.Start())
throw new Exception("Failed to start Pull process");}
}
I have the following questions:
(1). testHelloWorldNew.jar file is displaying the "Welcome to HelloWorld!" from Command Prompt. How can i either capture this message or the status of the execution of .jar file??
(2).How to make sure this process is executing correctly and closing?Can anybody throw some light on this?
Thanks.


