4 Replies - 9067 Views - Last Post: 17 April 2012 - 08:55 PM Rate Topic: -----

#1 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Running java from C#

Post icon  Posted 17 April 2012 - 07:38 PM

I just want to run a simple .jar file. I can't imagine why this has become so complicated for me.
My code looks like this:

            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "java.exe";
            //p.StartInfo.Arguments = " -jar rail.jar \"" + Path.GetTempPath() + "\\railtest.tmp" + "\" " + testString;
            p.Start();


Going to a command line I type "java.exe" and I get information that verifies that it exists. However any time I run this code it fails saying the file can't be found. What's going on here? Does Process not care about the computer's path variable or something? And if this is the case, how do I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Running java from C#

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Running java from C#

Posted 17 April 2012 - 07:44 PM

Is the file in the same folder as the executable? I think you could use Application.StartupPath + "\java.exe"
http://msdn.microsof...tartuppath.aspx
Was This Post Helpful? 1
  • +
  • -

#3 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Re: Running java from C#

Posted 17 April 2012 - 07:50 PM

No its not. java is anywhere on the computer. The exact location of java shouldn't be important. What matters is when you install java, the java binary is part of your path. On my computer its in C:\Windows\System32\
But on someone else's computer the executable could be located in a folder like C:\program files\java\bin, but that would be a part of the computer's path environment variable, so it wouldnt matter.

Nonetheless, even attempting to run C:\Windows\System32\java.exe fails. Even when I manually explore to the file and check that its there, even when I paste that path into the command line and check that its there.

I still get: An unhandled exception of type 'System.ComponentModel.Win32Exception' occured in System.dll Additional information: The system cannot find the file specified


Edit: I have made a discovery. If I change my target processor to x64 it sort of works, it doesnt complain that the file is missing, but no output is recorded. Somehow the x86 version of my program refuses to call on the x64 bit version of java.exe, and as far as I know that's the only one I've got so it just fails. Anyone have any idea how to fix this?

This works when I just copy java.exe directly to my program directory. Of course that's not a good solution, so if anyone has good solutions I'd like to hear them

This post has been edited by wd40bomber7: 17 April 2012 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,710
  • Joined: 08-June 10

Re: Running java from C#

Posted 17 April 2012 - 08:43 PM

I've heard about this one. Windows does some magic with it's system folders from x86 executables. When the x86 executable hits the system folder from the environmental variable (or Path, of course) it goes to the SysWOW64 folder instead of the System32 folder. (Makes sense, right? 64 bit assemblies in the System32 folder, and 32 bit assemblies in the SysWOW64 folder.)

Check this thread out:

http://social.msdn.m...8-0270298974ce/

The example in the solved post should help you out. He has the opposite problem from you (he has the 32 bit executable, not the 64 bit one), but the solution should fix you up just the same.

As to the output, how are you trying to get it? This is how you would normally go about it, if you wanted to print it to the console:

p.Start();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());


Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,710
  • Joined: 08-June 10

Re: Running java from C#

Posted 17 April 2012 - 08:55 PM

Ah, here's an MSDN article on what's going on:

http://msdn.microsof...7(v=vs.85).aspx

It's not just from the environmental variables or path, it's baked in behavior. The last part of the article shows a similar workaround.

Ha, I learned something new today. BTW, I'm featuring this topic, because it's a great question.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1