• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

executing java commands using a java program  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Instead from command prompt, I am trying to execute java commands like 'javac', 'java' using java program. I have tried Runtime.getRuntime().exec() but it only executes the DOS commands like 'del'. samples those i tried are:

with taking commands as String[],

commands[0] = "cmd";
commands[1] = "javac C:\\test.java";

commands[0] = "cmd";
commands[1] = "javac";
commands[2] = "C:\\test.java";

commands[0] = "cmd";
commands[1] = "set";
commands[2] = "path=C:\\Program Files\\IBM\\WebSphere Studio\\Application Developer IE\\v5.1\\runtimes\\base_v5\\java\\bin";
commands[3] = "javac C:\\test.java";

But none of above is working...no error/exception occurs but nothing happen. Just command prompt comes and disappears but no other action is happening....only DOS command such as following 'del' is working:

commands[0] = "cmd";
commands[1] = "del";
commands[2] = "C:\\test.java";

The lines used to execute are:

Runtime r= Runtime.getRuntime();
Process p=null;
}catch(Exception e)
System.out.println("Exception is:- "+e.getMessage());

Could you please tell me what is the way to execute java commands using java programming language?

My ultimate intention is to execute java commands (jar - xvf and other commands) to jar and unjar the files using java program instead command prompt.
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go:

Posts: 21745
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the Javaworld article "When Runtime().exec won't". You really should.

If you don't need the command window, you can simply skip the "cmd" part and execute the javac command directly:

About creating the JAR file, I wouldn't do that using command line but using the API itself. Check out the java.util.jar package; you can use a JarOutputStream around a FileOutputStream to write the entries. Writing an entry goes as follows:
1) create a ZipEntry
2) call putNextEntry()
3) write the entire contents of the file to put in the JAR file
4) repeat steps 2 and 3 for all other entries
5) close() the JarOutputStream
You can even add a Manifest.MF file using the Manifest class.

Unpacking the JAR file can be done using JarFile or JarInputStream.
Posts: 64494
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this website; there have been threads about using DOS commands with Runtime.exec() before, but you may have to go back a few years. Some of those commands are specific commands for the command line program, rather than invocations of executables. I can't remember the details, but there will be more in the old thread.

And I presume you have read when Runtime.exec() won't, Michael Daconta's classic article (which I see Rob has quoted too)?
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
how do I do my own kindle-like thing - without amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!