Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try to run commands in command prompt by using java

 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ItDoesntWorkIsUseless

Should not the String in red (file path) be escaped like "C:\\Java\\.." so on?
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, backslash must be escaped with another backslash.
Try this:
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And avoid coloured text, which makes it awkward to try to run that code.
 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@zandis thank you....its working!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic