Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using DOS commands in Java

 
Samuel Gmehlin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48953
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Samuel Gmehlin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic