• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

running dos commands through java

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I do it? And can I find the path where the class file is executing currently programatically during runtime.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anirban
Can I do it
Yes, you could write a batch file with the dos commands in it, and then use the exec() method in Runtime class to execute the batch file. I asked a similar question and Cindy Glass posted a link to an exellent article on the topic, unfortunatly I cant find the thread in question, if I do find it I'll post it here.
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dermot,
I tried what u said but it gave the following error:
An error occuredjava.io.IOException: CreateProcess: text error=2
I ran the batch file as follows :

The text.bat file is in the same directory as the class file.
 
Dermot Curley
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try adding the .bat extention to the String that is passed to exec i.e.
Runtime.getRuntime().exec("text.bat") ;
that should do it.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some DOS commands you may need to use the cmd.exe or command.exe syntax:


also:
You can get the JAVA_HOME propery which will tell you where java is installed.
String javahome = System.getProperties("java.home");
Then you can use reflection to find out the package that the class is in. Something like:
String thePackage = myObj.class.getPackage().toString();
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks dermot and cindy,
I tried using .bat extension also but it didn't work. What method did cindy gave i also tried that but it works only with running .exe files like java etc, not for general commands like :
cd, copy, move, del etc. How can I invoke this through java. If you are wondering why to do so, so I am trying to do all the file related functionality through Java, so I need this.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why in the world would you use Operating System commands to do this? What is wrong with using Java? Then it is OS independant.
So exactly what is wrong with
thisFile.renameTo(dest); //moves or renames
thisFile.delete(); //deletes
copy

etc?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic