Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Execution of Java program

C# Execution of Java program

Scheduled Pinned Locked Moved C#
csharpjavadiscussionworkspace
15 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jerry W Manweiler Ph D
    wrote on last edited by
    #1

    I'm working on a project that requires my C# code to execute a Java program provided to me by my client. I create a new ProcessStartInfo and setup the process as follows: psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; psi.FileName = the java executable to be run; psi.Arguments = the arguments for the code; psi.WindowStyle = hidden; I then execute the program using using (Process exeProcess = Process.Start(psi)) { exeProcess.WaitForExit(); } when I execute the process the code crashes without any information. Any thoughts on what I might be doing incorrect or any thoughts on trying to get more information about why the crash occurs. As a note, if I run this process outside my code in a command prompt then it works without failure. Thanks Jerry

    Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

    R N J 3 Replies Last reply
    0
    • J Jerry W Manweiler Ph D

      I'm working on a project that requires my C# code to execute a Java program provided to me by my client. I create a new ProcessStartInfo and setup the process as follows: psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; psi.FileName = the java executable to be run; psi.Arguments = the arguments for the code; psi.WindowStyle = hidden; I then execute the program using using (Process exeProcess = Process.Start(psi)) { exeProcess.WaitForExit(); } when I execute the process the code crashes without any information. Any thoughts on what I might be doing incorrect or any thoughts on trying to get more information about why the crash occurs. As a note, if I run this process outside my code in a command prompt then it works without failure. Thanks Jerry

      Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      Please Wrap your code in "pre" Tag to give better readability to other users.

      1 Reply Last reply
      0
      • J Jerry W Manweiler Ph D

        I'm working on a project that requires my C# code to execute a Java program provided to me by my client. I create a new ProcessStartInfo and setup the process as follows: psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; psi.FileName = the java executable to be run; psi.Arguments = the arguments for the code; psi.WindowStyle = hidden; I then execute the program using using (Process exeProcess = Process.Start(psi)) { exeProcess.WaitForExit(); } when I execute the process the code crashes without any information. Any thoughts on what I might be doing incorrect or any thoughts on trying to get more information about why the crash occurs. As a note, if I run this process outside my code in a command prompt then it works without failure. Thanks Jerry

        Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        The process is not exiting without any information, you are just not capturing it. I'm not sure what version you are using but the Process class doesn't have a static Start method, or one that returns a Process object that I can find.

        using(Process p = new Process())
        {
        try
        {
        p.StartInfo = psi;
        if(p.Start())
        {
        p.WaitForExit();
        }
        else
        {
        // Process did not start
        }
        }
        catch(Exception ex)
        {
        // p.ExitCode
        }
        }

        Also note the use of pre tags to format code snippets when posted.


        No comment

        J 1 Reply Last reply
        0
        • N Not Active

          The process is not exiting without any information, you are just not capturing it. I'm not sure what version you are using but the Process class doesn't have a static Start method, or one that returns a Process object that I can find.

          using(Process p = new Process())
          {
          try
          {
          p.StartInfo = psi;
          if(p.Start())
          {
          p.WaitForExit();
          }
          else
          {
          // Process did not start
          }
          }
          catch(Exception ex)
          {
          // p.ExitCode
          }
          }

          Also note the use of pre tags to format code snippets when posted.


          No comment

          J Offline
          J Offline
          Jerry W Manweiler Ph D
          wrote on last edited by
          #4

          I'm working in .NET 4.0 and there is a Process.Start(ProcessStartInfo) method available. OK if the process is exiting with information what do I need to do to capture that information? The C# code that I'm executing just simply crashes without any exceptions thrown or anything. I am working in the Visual Studio 2010 IDE and it just simply stops.

          Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

          N A 2 Replies Last reply
          0
          • J Jerry W Manweiler Ph D

            I'm working in .NET 4.0 and there is a Process.Start(ProcessStartInfo) method available. OK if the process is exiting with information what do I need to do to capture that information? The C# code that I'm executing just simply crashes without any exceptions thrown or anything. I am working in the Visual Studio 2010 IDE and it just simply stops.

            Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Indeed, I missed the overloaded method in the docs. :-O You should still be able to get the ExitCode. Is the Process being started? Did you try the provided sample?


            No comment

            J 1 Reply Last reply
            0
            • N Not Active

              Indeed, I missed the overloaded method in the docs. :-O You should still be able to get the ExitCode. Is the Process being started? Did you try the provided sample?


              No comment

              J Offline
              J Offline
              Jerry W Manweiler Ph D
              wrote on last edited by
              #6

              I based my code off of the provided sample. As far as I can tell the execute is occuring but when the process starts the system crashes.

              Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

              N 1 Reply Last reply
              0
              • J Jerry W Manweiler Ph D

                I based my code off of the provided sample. As far as I can tell the execute is occuring but when the process starts the system crashes.

                Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                Is there no exception being thrown? Does the application run on its own, not using Process?


                No comment

                J 1 Reply Last reply
                0
                • N Not Active

                  Is there no exception being thrown? Does the application run on its own, not using Process?


                  No comment

                  J Offline
                  J Offline
                  Jerry W Manweiler Ph D
                  wrote on last edited by
                  #8

                  As I indicated in my original post, There are no exceptions thrown and the process works fine outside the C# environment That is why I'm looking for help in trying to understand what is going on.

                  Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                  1 Reply Last reply
                  0
                  • J Jerry W Manweiler Ph D

                    I'm working on a project that requires my C# code to execute a Java program provided to me by my client. I create a new ProcessStartInfo and setup the process as follows: psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; psi.FileName = the java executable to be run; psi.Arguments = the arguments for the code; psi.WindowStyle = hidden; I then execute the program using using (Process exeProcess = Process.Start(psi)) { exeProcess.WaitForExit(); } when I execute the process the code crashes without any information. Any thoughts on what I might be doing incorrect or any thoughts on trying to get more information about why the crash occurs. As a note, if I run this process outside my code in a command prompt then it works without failure. Thanks Jerry

                    Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Jerry W. Manweiler, Ph.D. wrote:

                    when I execute the process the code crashes without any information.

                    That says nothing. A crash suggests a system (OS) exception occurred. I doubt that is the case. Either nothing at all happened or something did just not what you wanted.

                    Jerry W. Manweiler, Ph.D. wrote:

                    psi.FileName = the java executable to be run;
                    psi.Arguments = the arguments for the code;

                    In this context the above is non-sensical. There are two possibilities. 1. You are running a java application. 2. You are running a java executable jar. For the first it requires. 1. The java exectutable - java.exe 2. Appropriate class path 3. The FQN of the main class For the second it requires. 1. The java exectutable - java.exe 2. Appropriate jar 3. Appropriate command line options Regardless of the above the Process provides for the following A. The exe exit code B. Stdout C. Stderr The MOST likely failure is that the Process.ExitCode is 2 which means your "executable" (whatever that really is) is not pathed correctly or isn't even an executable (although then it is probably something besides 2.) If in fact java is running at all then there should be stdout/err output which, as noted above, you can extract for more information.

                    J 1 Reply Last reply
                    0
                    • J jschell

                      Jerry W. Manweiler, Ph.D. wrote:

                      when I execute the process the code crashes without any information.

                      That says nothing. A crash suggests a system (OS) exception occurred. I doubt that is the case. Either nothing at all happened or something did just not what you wanted.

                      Jerry W. Manweiler, Ph.D. wrote:

                      psi.FileName = the java executable to be run;
                      psi.Arguments = the arguments for the code;

                      In this context the above is non-sensical. There are two possibilities. 1. You are running a java application. 2. You are running a java executable jar. For the first it requires. 1. The java exectutable - java.exe 2. Appropriate class path 3. The FQN of the main class For the second it requires. 1. The java exectutable - java.exe 2. Appropriate jar 3. Appropriate command line options Regardless of the above the Process provides for the following A. The exe exit code B. Stdout C. Stderr The MOST likely failure is that the Process.ExitCode is 2 which means your "executable" (whatever that really is) is not pathed correctly or isn't even an executable (although then it is probably something besides 2.) If in fact java is running at all then there should be stdout/err output which, as noted above, you can extract for more information.

                      J Offline
                      J Offline
                      Jerry W Manweiler Ph D
                      wrote on last edited by
                      #10

                      I am running the 1st option that you indicate and provide the java application (java.exe) and the class that I am executing as one of the input parameters. Regardless of the above, the Process dies by killing all associated running processes including the IDE. The entire process (java class parms) does run just fine if I do it outside my code. So again the question is what in the code am I doing incorrectly or is it possible that you or others have run into this problem and figured out what is really going on. And to be very explicit - here is the actual code that I'm using:

                              DirectoryInfo di = new DirectoryInfo(path);
                              FileInfo\[\] files = di.GetFiles("\*.xml");
                              foreach (FileInfo file in files)
                              {
                                  Text = "Converting: " + file.Name;
                      
                                  String\[\] parms = new String\[1\];
                                  parms\[0\] = file.FullName;                
                                  
                                  ProcessStartInfo psi = new ProcessStartInfo();
                                  psi.CreateNoWindow = true;
                                  psi.UseShellExecute = false;
                                  psi.RedirectStandardError = true;
                                  psi.RedirectStandardOutput = true;
                                  psi.FileName = @"\\\\NASA\\GSFC\\CDF\\CDFML2CDF.exe";
                                  psi.Arguments = file.FullName;
                                  psi.WindowStyle = ProcessWindowStyle.Hidden;
                                  
                                  try
                                  {
                                      // Start the process with the info we specified.
                                      // Call WaitForExit and then the using statement will close.
                                      using (Process exeProcess = Process.Start(psi))
                                      {
                                          exeProcess.WaitForExit();
                                          Text = "    Processing successful for file = " + file.Name;
                                          Text = " ---- Process output Begin---";
                                          Text = exeProcess.StandardOutput.ReadToEnd();
                                          Text = " ---- Process output End---";
                                      }
                                  }
                                  catch
                                  {
                                      Text = "    Processing failed for file = " + file.Name;
                                  }
                              }
                      

                      when it executes

                      Process exeProcess = Process.Start(psi)

                      The IDE and all threads die. There is nothing logged in the System or application event logs. There is nothing output to the system - IT JUST CRASHES the IDE and Threads. Do you have any helpful ideas or suggestions?

                      Richard Andrew x64R P J 4 Replies Last reply
                      0
                      • J Jerry W Manweiler Ph D

                        I am running the 1st option that you indicate and provide the java application (java.exe) and the class that I am executing as one of the input parameters. Regardless of the above, the Process dies by killing all associated running processes including the IDE. The entire process (java class parms) does run just fine if I do it outside my code. So again the question is what in the code am I doing incorrectly or is it possible that you or others have run into this problem and figured out what is really going on. And to be very explicit - here is the actual code that I'm using:

                                DirectoryInfo di = new DirectoryInfo(path);
                                FileInfo\[\] files = di.GetFiles("\*.xml");
                                foreach (FileInfo file in files)
                                {
                                    Text = "Converting: " + file.Name;
                        
                                    String\[\] parms = new String\[1\];
                                    parms\[0\] = file.FullName;                
                                    
                                    ProcessStartInfo psi = new ProcessStartInfo();
                                    psi.CreateNoWindow = true;
                                    psi.UseShellExecute = false;
                                    psi.RedirectStandardError = true;
                                    psi.RedirectStandardOutput = true;
                                    psi.FileName = @"\\\\NASA\\GSFC\\CDF\\CDFML2CDF.exe";
                                    psi.Arguments = file.FullName;
                                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                                    
                                    try
                                    {
                                        // Start the process with the info we specified.
                                        // Call WaitForExit and then the using statement will close.
                                        using (Process exeProcess = Process.Start(psi))
                                        {
                                            exeProcess.WaitForExit();
                                            Text = "    Processing successful for file = " + file.Name;
                                            Text = " ---- Process output Begin---";
                                            Text = exeProcess.StandardOutput.ReadToEnd();
                                            Text = " ---- Process output End---";
                                        }
                                    }
                                    catch
                                    {
                                        Text = "    Processing failed for file = " + file.Name;
                                    }
                                }
                        

                        when it executes

                        Process exeProcess = Process.Start(psi)

                        The IDE and all threads die. There is nothing logged in the System or application event logs. There is nothing output to the system - IT JUST CRASHES the IDE and Threads. Do you have any helpful ideas or suggestions?

                        Richard Andrew x64R Offline
                        Richard Andrew x64R Offline
                        Richard Andrew x64
                        wrote on last edited by
                        #11

                        Hi Jerry, Try saying:

                                   psi.UseShellExecute = true;
                        

                        instead. (Really just a shot in the dark, but I think it's worth a try.)

                        The difficult we do right away... ...the impossible takes slightly longer.

                        1 Reply Last reply
                        0
                        • J Jerry W Manweiler Ph D

                          I am running the 1st option that you indicate and provide the java application (java.exe) and the class that I am executing as one of the input parameters. Regardless of the above, the Process dies by killing all associated running processes including the IDE. The entire process (java class parms) does run just fine if I do it outside my code. So again the question is what in the code am I doing incorrectly or is it possible that you or others have run into this problem and figured out what is really going on. And to be very explicit - here is the actual code that I'm using:

                                  DirectoryInfo di = new DirectoryInfo(path);
                                  FileInfo\[\] files = di.GetFiles("\*.xml");
                                  foreach (FileInfo file in files)
                                  {
                                      Text = "Converting: " + file.Name;
                          
                                      String\[\] parms = new String\[1\];
                                      parms\[0\] = file.FullName;                
                                      
                                      ProcessStartInfo psi = new ProcessStartInfo();
                                      psi.CreateNoWindow = true;
                                      psi.UseShellExecute = false;
                                      psi.RedirectStandardError = true;
                                      psi.RedirectStandardOutput = true;
                                      psi.FileName = @"\\\\NASA\\GSFC\\CDF\\CDFML2CDF.exe";
                                      psi.Arguments = file.FullName;
                                      psi.WindowStyle = ProcessWindowStyle.Hidden;
                                      
                                      try
                                      {
                                          // Start the process with the info we specified.
                                          // Call WaitForExit and then the using statement will close.
                                          using (Process exeProcess = Process.Start(psi))
                                          {
                                              exeProcess.WaitForExit();
                                              Text = "    Processing successful for file = " + file.Name;
                                              Text = " ---- Process output Begin---";
                                              Text = exeProcess.StandardOutput.ReadToEnd();
                                              Text = " ---- Process output End---";
                                          }
                                      }
                                      catch
                                      {
                                          Text = "    Processing failed for file = " + file.Name;
                                      }
                                  }
                          

                          when it executes

                          Process exeProcess = Process.Start(psi)

                          The IDE and all threads die. There is nothing logged in the System or application event logs. There is nothing output to the system - IT JUST CRASHES the IDE and Threads. Do you have any helpful ideas or suggestions?

                          Richard Andrew x64R Offline
                          Richard Andrew x64R Offline
                          Richard Andrew x64
                          wrote on last edited by
                          #12

                          Hi Jerry, I had another idea. If you're using Visual Studio 2010, you can step into the actual .NET framework code that is executing when you call Process.Start(). Just follow these steps: HOW TO: Debug .NET Framework Source[^]

                          The difficult we do right away... ...the impossible takes slightly longer.

                          1 Reply Last reply
                          0
                          • J Jerry W Manweiler Ph D

                            I am running the 1st option that you indicate and provide the java application (java.exe) and the class that I am executing as one of the input parameters. Regardless of the above, the Process dies by killing all associated running processes including the IDE. The entire process (java class parms) does run just fine if I do it outside my code. So again the question is what in the code am I doing incorrectly or is it possible that you or others have run into this problem and figured out what is really going on. And to be very explicit - here is the actual code that I'm using:

                                    DirectoryInfo di = new DirectoryInfo(path);
                                    FileInfo\[\] files = di.GetFiles("\*.xml");
                                    foreach (FileInfo file in files)
                                    {
                                        Text = "Converting: " + file.Name;
                            
                                        String\[\] parms = new String\[1\];
                                        parms\[0\] = file.FullName;                
                                        
                                        ProcessStartInfo psi = new ProcessStartInfo();
                                        psi.CreateNoWindow = true;
                                        psi.UseShellExecute = false;
                                        psi.RedirectStandardError = true;
                                        psi.RedirectStandardOutput = true;
                                        psi.FileName = @"\\\\NASA\\GSFC\\CDF\\CDFML2CDF.exe";
                                        psi.Arguments = file.FullName;
                                        psi.WindowStyle = ProcessWindowStyle.Hidden;
                                        
                                        try
                                        {
                                            // Start the process with the info we specified.
                                            // Call WaitForExit and then the using statement will close.
                                            using (Process exeProcess = Process.Start(psi))
                                            {
                                                exeProcess.WaitForExit();
                                                Text = "    Processing successful for file = " + file.Name;
                                                Text = " ---- Process output Begin---";
                                                Text = exeProcess.StandardOutput.ReadToEnd();
                                                Text = " ---- Process output End---";
                                            }
                                        }
                                        catch
                                        {
                                            Text = "    Processing failed for file = " + file.Name;
                                        }
                                    }
                            

                            when it executes

                            Process exeProcess = Process.Start(psi)

                            The IDE and all threads die. There is nothing logged in the System or application event logs. There is nothing output to the system - IT JUST CRASHES the IDE and Threads. Do you have any helpful ideas or suggestions?

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            I would try something like this instead:

                            psi.StartInfo.FileName=@"c:\javadir\java.exe";
                            psi.StartInfo.WorkingDirectory = @"\\NASA\GSFC\CDF\";
                            psi.StartInfo.Arguments=@"CDFML2CDF.exe";
                            psi.StartInfo.EnvironmentVariables.Add("CLASSPATH", "classpath goes here");

                            Forgive your enemies - it messes with their heads

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                            1 Reply Last reply
                            0
                            • J Jerry W Manweiler Ph D

                              I am running the 1st option that you indicate and provide the java application (java.exe) and the class that I am executing as one of the input parameters. Regardless of the above, the Process dies by killing all associated running processes including the IDE. The entire process (java class parms) does run just fine if I do it outside my code. So again the question is what in the code am I doing incorrectly or is it possible that you or others have run into this problem and figured out what is really going on. And to be very explicit - here is the actual code that I'm using:

                                      DirectoryInfo di = new DirectoryInfo(path);
                                      FileInfo\[\] files = di.GetFiles("\*.xml");
                                      foreach (FileInfo file in files)
                                      {
                                          Text = "Converting: " + file.Name;
                              
                                          String\[\] parms = new String\[1\];
                                          parms\[0\] = file.FullName;                
                                          
                                          ProcessStartInfo psi = new ProcessStartInfo();
                                          psi.CreateNoWindow = true;
                                          psi.UseShellExecute = false;
                                          psi.RedirectStandardError = true;
                                          psi.RedirectStandardOutput = true;
                                          psi.FileName = @"\\\\NASA\\GSFC\\CDF\\CDFML2CDF.exe";
                                          psi.Arguments = file.FullName;
                                          psi.WindowStyle = ProcessWindowStyle.Hidden;
                                          
                                          try
                                          {
                                              // Start the process with the info we specified.
                                              // Call WaitForExit and then the using statement will close.
                                              using (Process exeProcess = Process.Start(psi))
                                              {
                                                  exeProcess.WaitForExit();
                                                  Text = "    Processing successful for file = " + file.Name;
                                                  Text = " ---- Process output Begin---";
                                                  Text = exeProcess.StandardOutput.ReadToEnd();
                                                  Text = " ---- Process output End---";
                                              }
                                          }
                                          catch
                                          {
                                              Text = "    Processing failed for file = " + file.Name;
                                          }
                                      }
                              

                              when it executes

                              Process exeProcess = Process.Start(psi)

                              The IDE and all threads die. There is nothing logged in the System or application event logs. There is nothing output to the system - IT JUST CRASHES the IDE and Threads. Do you have any helpful ideas or suggestions?

                              J Offline
                              J Offline
                              jschell
                              wrote on last edited by
                              #14

                              Jerry W. Manweiler, Ph.D. wrote:

                              psi.FileName = @"\\NASA\GSFC\CDF\CDFML2CDF.exe";

                              That obviously is not java.

                              Jerry W. Manweiler, Ph.D. wrote:

                              The IDE and all threads die.

                              So you run the C# code in an IDE. And with in runs that line the IDE exits. Which is not a crash. Is the IDE Visual Studio? Which version? What happens if you replace the above line with another exe - like notepad (use the full path)?

                              1 Reply Last reply
                              0
                              • J Jerry W Manweiler Ph D

                                I'm working in .NET 4.0 and there is a Process.Start(ProcessStartInfo) method available. OK if the process is exiting with information what do I need to do to capture that information? The C# code that I'm executing just simply crashes without any exceptions thrown or anything. I am working in the Visual Studio 2010 IDE and it just simply stops.

                                Jerry W. Manweiler, Ph.D. Fundamental Technologies, LLC

                                A Offline
                                A Offline
                                Alan Balkany
                                wrote on last edited by
                                #15

                                Two suggestions: 1. Display the Exception message (and InnerException message). 2. After it crashes, look at the last entry in the Windows event viewer ([^] )

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups