How to run a *.exe file in Java

Closed Thread

Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

How to run a *.exe file in Java

 
0
  #1
Aug 10th, 2006
Greetings,

I was wondering if someone could teach me how to run an executable in java.

I have written a Java program that fills out some information, but I need to run a file that is normally run from a command prompt that is an executable. If someone could tell me how to do this, I would appreciate it. Thanks.


Sciocosmist
Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to run a *.exe file in Java

 
0
  #2
Aug 10th, 2006
Last edited by iamthwee; Aug 10th, 2006 at 12:06 pm.
*Voted best profile in the world*
Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #3
Aug 14th, 2006
Thank you for your help! This is close to what I needed. On the *.exe file it has a command line sequence of:

command=(path of program)/name of program.exe (upload file #1)=(path file#1) (upload file #2)=(path file#2) (output file name)=(name for output) /exit

That is to say that the variable command=the path of the *.exe program, space, first needed file, space, second file needed, space, output name, space, /exit.

The java command: Runtime.getRuntime().exec(command); //command is a variable

has a problem with the output file name becase it is technically not created yet. Would any one have a solution for how I would be able to have the output file included?
Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to run a *.exe file in Java

 
0
  #4
Aug 15th, 2006
>has a problem with the output file name becase it is technically not created yet. Would any one have a solution for how I would be able to have the output file included?

I would guess that the file has to be opened, written then closed before you can launch it as an executable. Is that what you mean?
*Voted best profile in the world*
Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #5
Aug 15th, 2006
A command prompt would come up and say 'Windows does not recognize (new file name)'


After a bit of investigation, I have found that the command for the runtime process needs to have a destination/working directory where the program is stored.

Here is what I am attempting to do:

String command="(path of program)/name of program.exe /(upload file #1)=(path file#1) /(upload file #2)=(path file#2) /(output file name)=(name for output) /exit"

That is to say that the variable command=the path of the *.exe program, space, forward slash, program recognition code, equals sign, first needed file, space, forward slash, program recognition code, equals sign, second file needed, space, forward slash, program recognition code, equals sign, output name, space, /exit.

The java command: Process p=Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ command); //command is a variable

p.waitFor();//to allow the program to terminate before proceeding.


Since this is an *.exe it needs to have certain addional files not listed in the original command line prompt from the Java code, in order to execute.

Could you also tell me how to set the correct directory so that the command line would know where to find the necessary files to execute the *.exe? I have tried to use the System.getenv(...) method, but that didn't work.

Again thank you, and anyone else willing to help!

--Sciocosmist
Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #6
Aug 16th, 2006
I am trying to run the program from a method as oppossed to using the main method. Could any one give me some advice on how to run the Runtime.getRunTime.exec(...) method for a *.exe file with three needed arguments without using the "public static void main (String[] args) method? I cannot use a main method to pass parameters in with the program that I am coding.

The first argument is a file that I have created with my Java program. The second argument is a file that I also have created with my Java program. The third argument is the output name of the *.exe file (the *.exe file writes a file based on the information from the other two files).
Quick reply to this message  
Join Date: Aug 2006
Posts: 3
Reputation: lybarger999 is an unknown quantity at this point 
Solved Threads: 0
lybarger999 lybarger999 is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #7
Aug 17th, 2006
I'm not sure I'm following the details of the problems you are having, but more generally, have a look at the [new] "ProcessBuilder" class in the standard java API docs. It gives you better control over the command line arguments and also the working directory that the process appears to run in. In other words, it is better than Runtime's exec.
Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #8
Aug 18th, 2006
The executable that I am trying to run is a MSDOS based program. I need to pass in parameters to the command line.

I have been able to get windows based *.exe files to run, but not an MSDOS application to run from the Java Code.

Here is the command that I need to run from a command prompt (and in the java program I am writing):

  1. C:\My Directory\My_exe.exe /cfg=my_cfg.cfg /err=my_xmd.xmd /xml=My_xml.xml /exit

Where both the *.cfg and the *.xmd are files that are read by the *.exe program. The *.xml is the name for the output file that the *.exe program produces.

This does work in a command prompt or command line, but it does not work in Java code (so far as I have tried).

Here is some of what I have tried:
Example 1:
  1. Process p=Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " +"C:\\My Directory\\My_exe.exe /cfg=my_cfg.cfg /err=my_xmd.xmd /xml=My_xml.xml /exit");
  2. Thread needle =new Thread();
  3. needle.sleep(((y/120)*1500+15000));//y is an int that is obtained from different sources throughout the program; I have checked it and it is fine.
  4. p.destroy();//By this time the process has closed and I can close the window. The waitFor() command does not work for my purposes.
Example 2:
  1. ProcessBuilder pb=new ProcessBuilder("cmd.exe /c start C:\\My Directory\\My_exe.exe /cfg=my_cfg.cfg /err=my_xmd.xmd /xml=My_xml.xml /exit");
  2. pb.directory(new File("C:\\My Directory\\My_exe.exe"));
  3. Process p=pb.start();
  4. Thread needle =new Thread();
  5. needle.sleep(((y/120)*1500+15000));//y is an int that is obtained from different sources throughout the program; I have checked it and it is fine.
  6. p.destroy();//By this time the process has closed and I can close the window. The waitFor() command does not work for my purposes.
Example 3:
  1. Process p=Runtime.getRuntime().exec("C:\\My Directory\\My_exe.exe /cfg=my_cfg.cfg /err=my_xmd.xmd /xml=My_xml.xml /exit");
  2. Thread needle =new Thread();
  3. needle.sleep(((y/120)*1500+15000));//y is an int that is obtained from different sources throughout the program; I have checked it and it is fine.
  4. p.destroy();//By this time the process has closed and I can close the window. The waitFor() command does not work for my purposes.
Example 4:
  1. Process p=Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " +"C:\\My Directory\\My_pif.pif");//The pif file contains the string as listed above
  2. Thread needle =new Thread();
  3. needle.sleep(((y/120)*1500+15000));//y is an int that is obtained from different sources throughout the program; I have checked it and it is fine.
  4. p.destroy();//By this time the process has closed and I can close the window. The waitFor() command does not work for my purposes.
  1. Process p=Runtime.getRuntime().exec("C:\\My Directory\\My_bat.bat");//the *.bat contains the string listed above.
  2. Thread needle =new Thread();
  3. needle.sleep(((y/120)*1500+15000));//y is an int that is obtained from different sources throughout the program; I have checked it and it is fine.
  4. p.destroy();//By this time the process has closed and I can close the window. The waitFor() command does not work for my purposes.

I have tried variations of each of these, but it has not yet been able to run. If someone could help me out, I would appreciate it.

Thank you for your help!
Quick reply to this message  
Join Date: Aug 2006
Posts: 19
Reputation: sciocosmist is an unknown quantity at this point 
Solved Threads: 0
sciocosmist sciocosmist is offline Offline
Newbie Poster

Re: How to run a *.exe file in Java

 
0
  #9
Sep 8th, 2006
After painstaking hours, I have finally found the solution. Thanks to everyone that helped me.
Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: How to run a *.exe file in Java

 
-2
  #10
Jul 13th, 2007
there's no .exe in java...
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Quick reply to this message  
Closed Thread

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC