Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Run dos cmds thru java prg  RSS feed

 
Sonal Bagmar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi frnds,
i have a .java file thru which i want to run few dos cmds like 'ren' , 'type' etc.
plz tell me a soln . i tried this one .it isn't working
p = r.exec("javac "+args[0]+".java");
p.waitFor();
System.out.println("Compiled..");
p = r.exec("type Hello2.java");
p.waitFor();
System.out.println("Exec..");
the above code compiles the file, but doesn't exec dos cmd
plz help me out
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do 'r' and 'p' represent?
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonal,
Here is a good artical on using Runtime.exec.
Hope it helps.
Chris.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to use java to invoke the DOS commands ren and type? You can do both of those from Java and by doing that, you wouldn't have to fiddle with trying to exec a DOS program (which isn't always easy to do) and you wouldn't lose platform independence.
You can rename a file with File.renameTo() and you can read the contents of a file and display it (the DOS "type" command) with one fairly simple method you could write.
Brian
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!