This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
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
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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: 24081
    
  15

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: 9033
    
  10
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: 24081
    
  15


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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to run a commandline command from a java program
 
Similar Threads
Apache foundations' prunsrv.exe issue
how to create exe file
How to run a java class from command line in eclipse
Running a .exe from java program
Running an .exe from a Java program