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

How to run a commandline command from a java program

Jyothsna Sri Y
Ranch Hand

Joined: Sep 25, 2003
Posts: 35
Hi,
I wish to run a commandline command(eg: dir or cls) from my java program. How is this possible?
I also wish to run another exe file from my program. I have tried the following code:
Runtime r = Runtime.getRuntime();
Process p = r.exec("./rasmol");
But this doesn't seem to work, even if the exe is an excutable command from the commandprompt.
Thanks,
Regards
Jyothsna.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,
This doesn't actually have anything to do with threads; I'm going to recommend that it be moved.
First, as far as running rasmol goes: this code should work, given the right assumptions. Can you give any more details regarding what happens?
Second, as to running commands like dir or cls: both of these are built into the "cmd" or "command" shell on Windows (which you have depends on the Windows version, of course.) Therefore, you have to run "cmd" and tell it to run the command:

Now, note that that output from this process won't go to the terminal but to a Java InputStream that you can read from; get it using p.getInputStream().
Also, I doubt that running "cmd /c cls" is going to do anything, but it might surprise you.


[Jess in Action][AskingGoodQuestions]
Jyothsna Sri Y
Ranch Hand

Joined: Sep 25, 2003
Posts: 35
Hi Ernest,
Thanks a ton for the immediate response. I'm trying it.
But could you also tell me how I can run a .exe(executable from the commandline) file from my java program on linux.
cheers
Jyothsna.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Moving to Java In General - Intermediate forum


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


But could you also tell me how I can run a .exe(executable from the commandline) file from my java program on linux.

The Java code is exactly the same as for Windows; you just have to name the appropriate executable (note that Linux executables don't end in ".exe".)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run a commandline command from a java program