• 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 ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

try to run commands in command prompt by using java  RSS feed

 
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
 
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?
 
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:
 
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?



 
Marshal
Posts: 64483
225
  • 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!
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!