1

Possible Duplicate:
How to run Unix shell script from java code?

I know you can use Runtime.exec() or a ProcessBuilder to execute native commands on windows in this way:

cmd /c command_goes_here

But what is the quivelant of this on UNIX?

Or will all commands work straight through Java? Does it have internal terminal commands like Windows does?

Community
  • 1
  • 1
bgroenks
  • 1,859
  • 5
  • 34
  • 63

3 Answers3

0

It's called Runtime.exec(). That's sort of the point of Java -- write once run anywhere.

(Yes, yes, the same script won't run everywhere; that's what Runtime is for, to give you access to the underlying system.)

Java does not, however, have internal mechanisms for controlling the screen. For that you need to resort of the usual AWT/SWT/Swing etc routines.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • But... will using Runtime.exec() to run UNIX commands work for ALL UNIX commands? Or are there commands that are internal to the terminal? – bgroenks Dec 04 '11 at 21:01
0

My understanding is that Java exec is based off of the Posix exec* calls. So presumably you need to format the native command more or less the same way you'd do one in Posix (though I don't know if you'd need to specify the command name twice like in Posix -- I've never tried Java exec under *nix).

The significant thing to note is that in *nix the individual commands like ls stand alone, whereas in Windows commands like dir are really just parameters to CMD.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

As others have pointed out, running Runtime.exec() will help you. But beware! there are several considerations to take into account for successfully running a command from Java. Be sure of reading this article first, it explains all you need to know.

Óscar López
  • 232,561
  • 37
  • 312
  • 386