File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes trying to run a linux commands from a java app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "trying to run a linux commands from a java app" Watch "trying to run a linux commands from a java app" New topic
Author

trying to run a linux commands from a java app

sribu sribu
Greenhorn

Joined: May 02, 2006
Posts: 2
Hi,
I am trying to run a linux commands from a java app.The below code works for executing windows commands in java application,but how can i do in Linux. Please help me it very urgent.

Thanks,

try
{
String osName = System.getProperty("os.name" );
System.out.println(" OS NAME is "+osName);
// String[] cmd = new String[3];

if( osName.equals( "Windows 2000" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args;
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec();
// any error message?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well - that depends on the command to run of course.
I.e.:

run it like that:


btw.: OS NAME is Linux
[ May 03, 2006: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The equivalent of "cmd /c" is "sh -c". A command line like

sh -c "foo > outputfile < inputfile"

lets that last argument be interpreted by the shell as if it were typed at the shell prompt.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to run a linux commands from a java app