jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Using DOS commands in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using DOS commands in Java" Watch "Using DOS commands in Java" New topic
Author

Using DOS commands in Java

Samuel Gmehlin
Greenhorn

Joined: Jul 21, 2011
Posts: 3
Hi !

I'm using some DOS commands in my Java program and I don't understand why it doesn't execute my command when I run my program.
Here's my piece of code :



Basically, I want to use the command "jar" in order to unzip a WAR file (which is actually a ZIP). When execute the program, it does nothing at all. No unzipped folder, nothing.
It's working well if I replace my String com with, for instance :



It just prints the whole path as expected.

Does anybody know why ? Did I not write the correct syntax ? Then, why does it work when I type it exactly the same in the command prompt ?

Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40030
    
  28
How are you emptying the error Stream? Why have you not set up Threads to empty those Streams? Don't go near Runtime.exec until you have read and understood the classic article by Michael Daconta "When Runtime.exec() won't". Look up the ProcessBuilder class too, which makes Processes easier to handle.

I suspect you need to divide your arguments String into the name of the executable you wish to invoke, and the arguments, separated by commas, so as to make a String[] of arguments.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Samuel Gmehlin wrote:

Basically, I want to use the command "jar" in order to unzip a WAR file (which is actually a ZIP).


You don't need Runtime.exec()/ProcessBuilder to do that. As you said, 'war' files are just 'zip' files so you can use http://download.oracle.com/javase/1,5.0/docs/api/java/util/zip/ZipFile.html


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Samuel Gmehlin
Greenhorn

Joined: Jul 21, 2011
Posts: 3
Thanks for your replies.

I have read parts of the article and my problem is that I gave the exec method one String argument, like if I was writing it on the command line. So I tried to split it but the result is the same.

As I understood, I don't need the "cmd /C" in front of the command since "jar" is not a windows executable.



And I get :

java.io.IOException: Cannot run program ""C:\Program": CreateProcess error=5, Access denied
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at script.main(script.java:21)
Caused by: java.io.IOException: CreateProcess error=5, Access denied
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more


Apparently, it doesn't recognizes the whole path "\"%JAVA_HOME%/bin/jar\" represents. Because my Java directory is in Program Files, and Program Files contains a space, it got truncated. But to bypass this problem, I added quotes around it and it should work normally! So my conclusion is that Runtime.exec() can't translate my JAVA_HOME environment variable...am I correct ?

@James Sabre:
I'm looking into it now, but does it allow to modify a file inside a ZIP archive as well ? Because it seems you can mainly read it only.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

First of all, you're calling the exec method incorrectly. The first parameter should be the full command, the second String[] is for environment properties.

If you use a String[] for the first command, you don't need to worry about escaping. The code then becomes this (note: I also fixed the incorrect usage of /. Windows uses \):
However, this will still not work. That's because %JAVA_HOME% is not expanded like this, in contrast to using CMD /C. Fortunately, there is a quick and easy fix: use System.getProperty("java.home"):

OK, now we got that finished, it's still not going to work. That's because JAR is not a tool that comes with the JRE but with the JDK. The java.home system property uses the JRE; either the regular one, or the one installed with the JDK. So we're back to using CMD /C.

There is one more alternative though - don't use an external process, but do it from Java code. Inside the java.util.jar and java.util.zip packages there are a few classes that you can use to unpack a JAR or WAR file. I'd start with JarFile.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Samuel Gmehlin wrote:
@James Sabre:
I'm looking into it now, but does it allow to modify a file inside a ZIP archive as well ? Because it seems you can mainly read it only.


I'm willing to bet the behind the scenes 'jar' uses the Java jar/zip classes. I can both create and read 'zip', 'jar' and 'war' files using the standard Java API so I can extract the content, modify one of the entries and then re-(zip/jar/war) .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Samuel Gmehlin wrote:I'm looking into it now, but does it allow to modify a file inside a ZIP archive as well ? Because it seems you can mainly read it only.


There isn't anything which allows you to modify a file inside a ZIP archive.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

There are some tools that make it look like it's possible, but these just create a new ZIP file, then overwrite the original with the new one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using DOS commands in Java