469,631 Members | 1,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Help! C# calling Java Program

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.
Nov 15 '05 #1
6 8913
David <no*@vailable.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David <no*@vailable.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
David <no*@vailable.com> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David <no*@vailable.com> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
David <no*@vailable.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Sorted it, I've done away with the batch file.

Thanks Jon, you MVP's are great :-)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David <no*@vailable.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
4 posts views Thread by Sarir Khamsi | last post: by
2 posts views Thread by Sudheer Kareem | last post: by
6 posts views Thread by d.warnermurray | last post: by
3 posts views Thread by Colin J. Williams | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.