• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to run a linux commands from a java app

 
sribu sribu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic