Help! C# calling Java Program

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi,

I am writing a C# program and want to run a java application and pass it a
filename as a parameter. I want to be able to write a method in C# that
will run this Java app for me. Eg. I want to run the following command:

java MyApplication filename.extension

Can anyone help me?

Many thanks,
David.
 
David said:
I am writing a C# program and want to run a java application and pass it a
filename as a parameter. I want to be able to write a method in C# that
will run this Java app for me. Eg. I want to run the following command:

java MyApplication filename.extension

Can anyone help me?

Sure - look at the System.Diagnostics.Process and ProcessStartInfo
classes. The "about" page for ProcessStartInfo gives a good example.
 
Thanks for that Jon,
I have written the following code, but I am having a few problems. Does the
java program need to be an executable to run or is there any way I can
simply run a "java program_name" style command?

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample{

public class MyProcess{

public static void Main(){
try{
MyProcess myProcess = new MyProcess();
Process.Start("java XMLParse filename.txt"); }
catch(Exception e){
Console.WriteLine(e); }
}
}
}

Cheers,
David.
 
David said:
Thanks for that Jon,
I have written the following code, but I am having a few problems. Does the
java program need to be an executable to run or is there any way I can
simply run a "java program_name" style command?

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample{

public class MyProcess{

public static void Main(){
try{
MyProcess myProcess = new MyProcess();
Process.Start("java XMLParse filename.txt"); }
catch(Exception e){
Console.WriteLine(e); }
}
}
}

Well, what problems are you having? Is it managing to find Java to
start with?

Rather than use the version of Process.Start which takes a single
string parameter, I suggest you use the version which takes two
parameters - one for the executable and one for the arguments.

From the documentation for the version you called:

<quote>
This overload does not allow command-line arguments for the process.
</quote>
 
Thanks Jon,

I took your advice and I run a batch file now instead of "java myApp" and
use the double parameter method.

Cheers for your help,
David.
 
David said:
I took your advice and I run a batch file now instead of "java myApp" and
use the double parameter method.

Hang on though - I certainly didn't suggest a batch file! That strikes
me as horribly ugly. Just use the double parameter method with "java"
as the first parameter and "XMLParse filename.txt" as the second
parameter.
 
Back
Top