Results 1 to 9 of 9
Thread: Runing .exe from Java
- 11-16-2011, 09:41 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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()); } }
- 11-16-2011, 10:36 PM #2
Re: Runing .exe from Java
Google pitfalls of runtime exec to learn more about the numerous known issues.
- 11-16-2011, 10:38 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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.
- 11-16-2011, 11:38 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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.
- 11-17-2011, 12:52 AM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Runing .exe from Java
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();
- 11-17-2011, 02:37 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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
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.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();
Thanks
- 11-17-2011, 03:15 AM #7
Re: Runing .exe from Java
Did you try reading the API to see what methods the class does have?
- 11-17-2011, 04:26 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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.
- 11-17-2011, 04:15 PM #9
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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
-
runing Java program increases continuing memory usage
By henry123 in forum Advanced JavaReplies: 12Last Post: 02-25-2011, 02:09 PM -
error runing a java application
By samjesse in forum Java AppletsReplies: 5Last Post: 11-05-2009, 03:44 AM -
Inconvinience at Runing an eclipse application
By smalcat in forum EclipseReplies: 0Last Post: 11-11-2008, 03:08 PM -
Runing 44 Java Files Easyer
By crigur in forum New To JavaReplies: 5Last Post: 09-23-2008, 03:35 AM -
Trouble in Runing
By kavithaprabhaker in forum New To JavaReplies: 5Last Post: 05-13-2008, 05:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks