-3

I want to make a java web interface that run bash process in the background. So, in java how can I run linux command like "ls -al" or run bash script. Is there any package or java plugin that able to invoke OS command in JAVA?

Thanks all

Kenny Basuki
  • 625
  • 4
  • 11
  • 27

2 Answers2

0

As I showed here: Compiling a C Source through javacode using gcc

You could use ProcessBuilder to do that:

String directory = "/home/user";
String[] commands = {"ls", "-al"};

ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File(directory));
pb.command(commands);

Process p = pb.start();

OuputStream out = p.getOutputStream(); // to send other commands/data
InputStream in = p.getInputStream(); // to read console response
// process the streams
Community
  • 1
  • 1
D.Kastier
  • 2,640
  • 3
  • 25
  • 40
0

As already shown here

You can use the Runtime.exec() method.

Community
  • 1
  • 1
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57