- I want to execute a jar file from within the C#.net application.The jar file is a console application. The jar file is present in the same directory as the C# application. 2. I want to check if jdk is installed frm within C#.net applcation How can i do that?
views:
306answers:
3Use
Process
to startjava
process passing arguments-jar yourjar.jar
.JDKs is usually installed in
c:\Program Files\Java\jdk...
. check if this folder exists, it should fit most cases. Or checkHKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
in the registry.
Have batch file which contains whatever you need to start your java app.
E.g. "javaw -classpath path-of-jar"
System.Diagnostics.Process.Start("batchfile");
More here
For question 2, elder_george's suggestion should work.
If the java/bin folder is in the environment path, you might try:
System.Diagnostics.Process.Start("java.exe", "-version");
If no exception, you probably have a valid java.exe file.
The c# method
Process::Start(program, argumentString)
Can be used to start arbitrary applications, so if you know the command to start Java app from the command line then you can deduce the arguments to Process:Start().
The Java command will be
java -jar <jarfile> {and maybe <mainClass>}
So the issues here are
- is java available, is it on your path?
- what is the path of the jarfile
- what is the name of the class in the jar file implementing main? If the folks delivering the jarfile have been friendly then the JAR's manifest specifies the name of the main class so you won't need that argument. Otherwise, we'd hope it was documented somewhere.
If Java has been installed nicely then it should already be on your PATH. Try it from the command line. I think it's reasonable to pre-req that java be installed, so in your c# app I would just assume that Java is available and attempt to start it, then catch any failure with code such as:
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
etc.