aspose file tools*
The moose likes Java in General and the fly likes try to run commands in command prompt by using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "try to run commands in command prompt by using java" Watch "try to run commands in command prompt by using java" New topic
Author

try to run commands in command prompt by using java

naresh kumar mallala
Greenhorn

Joined: Jun 13, 2011
Posts: 21
Hi Friends....
Iam trying to execute a command by java wihout using command prompt.....and in that i need to set the directory also.
i tried all most all codes i know but all are failed....can any one gimme a suggestion plzz

My code is:

public class FinalCP
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("cmd C:\Java\jdk1.5.0_19\bin jar xf epic.jar");
//red color text is directory structure i have to set and green is command i want to execute
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}

}
catch(IOException e1) {}
catch(InterruptedException e2) {}

System.out.println("Done");
}
}

thank you
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please ItDoesntWorkIsUseless

Should not the String in red (file path) be escaped like "C:\\Java\\.." so on?
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Yes, backslash must be escaped with another backslash.
Try this:
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Typical case of "using the golden hammer".

You are writing a piece of code (executed with 'java FinalCP') that executes another piece of code that could be executed directly on the command line with 'jar xf epic.jar'.

Ask yourself this question: What do I want to do with the stuff in epic.jar? Can I instantiate a class in that application instead?



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
And avoid coloured text, which makes it awkward to try to run that code.
naresh kumar mallala
Greenhorn

Joined: Jun 13, 2011
Posts: 21
@lindstrom
ya....i know the command jar xf filename.jar
but i need to create application in java and this jar extraction in one step in that....so i want to do it also by using java program

thank you
naresh kumar mallala
Greenhorn

Joined: Jun 13, 2011
Posts: 21
@zandis thank you....its working!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try to run commands in command prompt by using java
 
Similar Threads
Jar packager
Help with coding
Program help
problems in subpackage and imports
How to import classes in a subpackage from another sub-package?