Results 1 to 9 of 9
  1. #1
    Java_B is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Runing .exe from Java

    Hello All,

    I'm a beginner as far as Java goes, so please be kind. The only reason I'm getting acquainted with it is because I must, I'd rather stick to my few skills in C. The question here is very simple, I'm trying to run this .exe file located in a certain directory in my Windows machine. I finally managed how to tell Java to run it (took me 6 hours to figure out how to refer to a path when it has spaces between words), but now the .exe won't find any of the files I intend to manipulate (Within the windows file system). If I run the program from command prompt or even by double clicking, it works just fine. If you have suggestions feel free to modify the code, here's everything I've tried:

    Java Code:
    	  public static void Run_RK4(){
    		  
    		  try { 
    			  System.setProperty("user.dir", "C:\\Docume~1\\AndresC\\MyDocu~1\\Norwood\\Lumped_parameter\\C_Code"); //Failed attempt to set current directory
    			  Runtime rt=Runtime.getRuntime();
                              
                              //1st Try
    			  //String rk4path="cmd.exe /C start C:\\Docume~1\\AndresC\\MyDocu~1\\Norwood\\Lumped_parameter\\C_Code\\Norwood_Main_Read-Write.exe";
    			  //2nd Try
                              //String[] rk4path = {"cmd.exe", "/C", "start", "C:\\Docume~1\\AndresC\\My Documents\\Norwood\\Lumped_parameter\\C_Code\\Norwood_Main_Read-Write.exe"};
    			  //3rd Try
                              //String[] rk4path = new String[4];
    			  //rk4path[0] = "cmd.exe";
    			  //rk4path[1] = "/C";
    			  //rk4path[2] = "start";
    			  //rk4path[3] = "C:\\Docume~1\\AndresC\\MyDocu~1\\Norwood\\Lumped_parameter\\C_Code\\Norwood_Main_Read-Write.exe";
    			  
    		          rt.exec(rk4path);
    
    			  //(B) Calling an .exe from java 
    			  //rt.exec("C:\\WINDOWS\\NOTEPAD.exe");
    			  //rt.exec("C:\\Docume~1\\AndresC\\MyDocu~1\\Norwood\\Lumped_parameter\\C_Code\\Norwood_Main_Read-Write.exe");
    		      } 
    			  catch(Throwable t) 
    			  { 
    			  System.out.print(t.getMessage()); 
    			  }
                   }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Runing .exe from Java

    Google pitfalls of runtime exec to learn more about the numerous known issues.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Runing .exe from Java

    Try running your java application from the same directory as the your exec application, passing the exec name rather than the full path.

  4. #4
    Java_B is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Runing .exe from Java

    Even if I figure out how to use the GoodWindowsExec.java in the reference there's no guarantee my *.exe is not going to behave the same way, from what I can see.
    As far as changing the directory, not an option since the Java code is actually a macro that I use to control a commercial Computational Fluid Dynamics software. What goes on internally is beyond me. I can't believe doing such a SIMPLE, EVERY DAY task is that counter intuitive, even the more arcaic C language works just fine.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Runing .exe from Java

    Quote Originally Posted by Java_B View Post
    Even if I figure out how to use the GoodWindowsExec.java in the reference there's no guarantee my *.exe is not going to behave the same way, from what I can see.
    As far as changing the directory, not an option since the Java code is actually a macro that I use to control a commercial Computational Fluid Dynamics software. What goes on internally is beyond me. I can't believe doing such a SIMPLE, EVERY DAY task is that counter intuitive, even the more arcaic C language works just fine.
    I don't really understand the argument about moving the java code, but be that as it may the problem here is that the exe you are running looks for and/or writes files relative to its launch directory...that launch directory is that of the java application. Move the java application, problem solved.

    That being said, a better alternative that I just discovered would to see if using a ProcessBuilder solve the problem.
    ProcessBuilder (Java Platform SE 6)
    Sets its directory to that of the application, then call the application directly (rather than using the full path)
    Java Code:
    ProcessBuilder pb = new ProcessBuilder("yourappname");
    pb.setDirectory(new File("my app directory"));
    Process p = pb.start();

  6. #6
    Java_B is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Runing .exe from Java

    doWhile,

    Thanks for the advice. I'm using the latest Eclipse to compile. Your second line pb.setDirectory is not defined for the type Process Builder. Here's what I have

    Java Code:
    			  ProcessBuilder pb = new ProcessBuilder("Norwood_Main_Read-Write.exe");
    			  pb.setdirectory(new File("C:\\Documents and Settings\\AndresC\\My Documents\\Norwood\\Lumped_parameter\\C_Code"));
    			  Process p = pb.start();
    I tried deleting the set, which will allow the program to run, but doesn't run the .exe file. Please pardon if I'm missing something obvious here, I'm a complete noob.

    Thanks

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Runing .exe from Java

    Did you try reading the API to see what methods the class does have?

  8. #8
    Java_B is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Runing .exe from Java

    Looked at the whole thing Java Platform SE 6 couldn't find anything relating the instruction set with directory.
    My own ignorance aggravates me, but seriously guys, this issue should have a simple solution. Is this really that uncommon? How about the equivalent of chdir, how hard is that in Java.

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Runing .exe from Java

    setdirectory != setDirectory

    This does a similar thing to the System.setProperty method call you make in your first code snippet (which I seemed to have overlooked on my first look at your code). After setting the correct directory make the application call by name not by full application path.

Similar Threads

  1. runing Java program increases continuing memory usage
    By henry123 in forum Advanced Java
    Replies: 12
    Last Post: 02-25-2011, 02:09 PM
  2. error runing a java application
    By samjesse in forum Java Applets
    Replies: 5
    Last Post: 11-05-2009, 03:44 AM
  3. Replies: 0
    Last Post: 11-11-2008, 03:08 PM
  4. Runing 44 Java Files Easyer
    By crigur in forum New To Java
    Replies: 5
    Last Post: 09-23-2008, 03:35 AM
  5. Trouble in Runing
    By kavithaprabhaker in forum New To Java
    Replies: 5
    Last Post: 05-13-2008, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •