none
How to execute a .Jar file

    Question

  • I have a .Jar file that when executed in CMD works. What I want is to do it in a C# application. What would be the code to do it? I'd like explanation of the code too so I understand it.
    Thursday, November 18, 2010 6:02 PM

Answers

  • Welcome to the MSDN Forums.

    Try

    System.Diagnostics.Process.Start("theFile.jar")
    

    Regards,

    Yasser


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, November 18, 2010 6:07 PM
  • You would either have to add @ at the beginning of the string, or included double forward slashes.

    System.Diagnostics.Process.Start(@"C:\Users\Jeanie\workspace\mylibrary.jar")

    or

    System.Diagnostics.Process.Start("C:\\Users\\Jeanie\\workspace\\mylibrary.jar")

    Thursday, November 18, 2010 7:03 PM
  • When debugging, it pops up a console window for less than a second then disappears which has content in it. Assuming this could be the .jar but what I want is for it to be displayed in the Windows Form app.

    You need to capture the output of the process.

    ProcessStartInfo startInfo = new ProcessStartInfo("C:\\javaInstallationPath\\java.exe",
                             "-jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar");
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    Process process = Process.Start(startInfo);
    
    textBox1.Text = process.StandardOutput.ReadToEnd();
    
    

     

    Friday, November 19, 2010 12:06 AM

All replies

  • Welcome to the MSDN Forums.

    Try

    System.Diagnostics.Process.Start("theFile.jar")
    

    Regards,

    Yasser


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, November 18, 2010 6:07 PM
  • using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Diagnostics.Process.Start("C:\Users\Jeanie\workspace\mylibrary.jar")
    
    namespace javaExecution
    {
     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
      }
     }
    }
    brings up an "unexpected ;" and 4 x "Unrecognized escape sequence"

    Could you be more specific as to what I have to do?

    System.Diagnostics.Process.Start("theFile.jar"
    
    
    )

    This means nothing to me.. I don't know what it does, it seems pretty explanatory as to what it does but it didn't work.
    Thursday, November 18, 2010 6:27 PM
  • Sure
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace javaExecution
    {
     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
       System.Diagnostics.Process.Start("C:\Users\Jeanie\workspace\mylibrary.jar")
      }
     }
    }
    

    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, November 18, 2010 6:31 PM
  • Still get the "Unrecognized escape sequence"
    Thursday, November 18, 2010 6:38 PM
  • I have tried this "System.Diagnostics.Process.Start("java -jar mylibrary.jar");" but it says "The system cannot find the file specified" I've included the .jar to the project. Using Add > Existing item
    Thursday, November 18, 2010 7:03 PM
  • You would either have to add @ at the beginning of the string, or included double forward slashes.

    System.Diagnostics.Process.Start(@"C:\Users\Jeanie\workspace\mylibrary.jar")

    or

    System.Diagnostics.Process.Start("C:\\Users\\Jeanie\\workspace\\mylibrary.jar")

    Thursday, November 18, 2010 7:03 PM
  • This doesn't execute it, it just opens the .jar in winrar. :( When using the following command "java -jar mylibrary.jar" in CMD, it prints the results it should. I want it to do that in C#.
    Thursday, November 18, 2010 7:09 PM
  • So make the Process Start execute the cmd like you would from a command prompt.   You may have to include the fully qualified location of the jar file.

    System.Diagnostics.Process.Start("java -jar mylib.jar");
    
    Thursday, November 18, 2010 7:19 PM
  • Yeah but then it says it can't find the path. "System.Diagnostics.Process.Start("java -jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar");" when it's exactly as it would be in CMD.
    Thursday, November 18, 2010 7:25 PM
  • It makes no sense... it's right file path. :'(
    Thursday, November 18, 2010 7:50 PM
  • It makes no sense... it's right file path. :'(


    Hi again :)

    It takes two parameters, the first is the executable file (full path if it's folder is not declared in environment variables!), the second is parameters, so please try

    System.Diagnostics.Process.Start("C:\\javaInstallationPath\\java.exe", "-jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar");//javaInstallationPath can be something like 'C:\Program Files\Java'
    

    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, November 18, 2010 8:01 PM
  • Now we have gotten further.

     

    When debugging, it pops up a console window for less than a second then disappears which has content in it. Assuming this could be the .jar but what I want is for it to be displayed in the Windows Form app.

    Thursday, November 18, 2010 8:06 PM
  • I am not sure if it's possible to run a jar file on a form surface, you only can run it as a process in my experience, sorry.
    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Thursday, November 18, 2010 8:19 PM
  • Damn. I'm new to Java too, wanted to run a java project within a C# like overtime, if my knowledge were ever to expand to game creation in Java, I would allow people to download it and play it without needing to open a web browser. I'm guessing my time attempting to do that is over. :(

    Thursday, November 18, 2010 8:28 PM
  • set the working directory, set the java bin path in environment variables and it should work.

     

     

     

     ProcessStartInfo startInfo = new ProcessStartInfo("C:\\javaInstallationPath\\java.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Minimized; startInfo.WorkingDirectory = "c:\\Users\\Jeanie\\workspace" startInfo.Arguments = "-jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar";
    Process.Start(startInfo);

     


    mole on left eye
    Thursday, November 18, 2010 9:53 PM
  • When debugging, it pops up a console window for less than a second then disappears which has content in it. Assuming this could be the .jar but what I want is for it to be displayed in the Windows Form app.

    You need to capture the output of the process.

    ProcessStartInfo startInfo = new ProcessStartInfo("C:\\javaInstallationPath\\java.exe",
                             "-jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar");
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    Process process = Process.Start(startInfo);
    
    textBox1.Text = process.StandardOutput.ReadToEnd();
    
    

     

    Friday, November 19, 2010 12:06 AM
  • When debugging, it pops up a console window for less than a second then disappears which has content in it. Assuming this could be the .jar but what I want is for it to be displayed in the Windows Form app.

    You need to capture the output of the process.

    ProcessStartInfo startInfo = new
     ProcessStartInfo("C:\\javaInstallationPath\\java.exe"
    ,
    "-jar C:\\Users\\Jeanie\\workspace\\mylibrary.jar" );
    startInfo.UseShellExecute = false ;
    startInfo.RedirectStandardOutput = true ;
    Process process = Process.Start(startInfo);

    textBox1.Text = process.StandardOutput.ReadToEnd();

     

    You can use the lines above, just don't forget to put your .jar path betwen "" if it have spaces.
    Tuesday, November 30, 2010 3:46 PM