0

I want to run different commands which can be executed on my command prompt or terminal through Java.

I did search few place but did not get appropriate reply.

I want to run a compiler which is set in the environment as VISAGE_HOME as well as run GRADLE so as to do all my build tasks.

I want to invoke all these commands from within Java Program.

Since it is a swing application I would like to invoke these commands on click of button or some other events.

My Problem is that I am not able to program this :( .

Neither do I know an API which would do this. I went through some sample codes but most of them have same kind of example codes of executing the shell commands or command prompt commands. None showed me to do the above stuff.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85
  • I think everything is described [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html), there are another tips for concrete native os – mKorbel Oct 03 '12 at 23:14

5 Answers5

2

Have a look at ProcessBuilder. The Process object it returns has a waitFor method so you can wait for the process to finish. Then you can start your next process.

For example

Process p = new ProcessBuilder("runYourCommand").start();
InputStream in = process.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr);
String inputLine;
while ((inputLine = br.readLine()) != null) {
  System.out.println(inputLine);
}
p.waitFor();

Another interesting method on ProcessBuilder is environment(). This will return the environment variables that you can access. From the API docs

Map<String, String> env = pb.environment();  
env.put("VAR1", "myValue");
RNJ
  • 15,272
  • 18
  • 86
  • 131
  • Nice mention of process builder. Add in some IO (inputStream at least) examples and I'll give you a vote up ;) – MadProgrammer Oct 03 '12 at 20:29
  • @MadProgrammer I added some more ;) Also found the environment method which I didnt know about originally which might help the questioner. – RNJ Oct 04 '12 at 07:35
1

Something like this:

String cmd = "gedit";

Runtime run = Runtime.getRuntime();

Process pr = run.exec(cmd);
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
1

Firstly, ProcessBuilder is your friend...

You could have a look at;

Secondly, You will need to use System.getenv to find the value of the specified environment variable and substitute it yourself.

nb: Thanks to Guillaume Polet for pointing out that the Process will automatically include the path environment variable to find commands.

Also, remember, DO NOT EXECUTE ANY BLOCKING PROCESS ON THE EDT.

Executing external commands are inherently blocking actions, while not explicitly, taking into account needing to consume the output of the process or wanting to know about the processes termination, these would require you to perform some kind of blocking action. Don't do this on the EDT. It will cause you program to appear as if it's hung.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • ok. Then will this be able to help me call the other tasks related to gradle or any other thing? – Shiv Kumar Ganesh Oct 03 '12 at 20:23
  • If you can run it from the command line, you can run it through `ProcessBuilder`. You need to take into account that you will need to consume the "output" from the process (you probably want to know about this any) as some processes will appear to "hang" if you don't – MadProgrammer Oct 03 '12 at 20:24
  • There are some caveats when trying to make system calls, but external, executable programs should not be a problem – MadProgrammer Oct 03 '12 at 20:26
  • Is this the way IDE's do this thing as in build/compile/run? – Shiv Kumar Ganesh Oct 03 '12 at 20:32
  • 1
    AFAIK, and according to your 2 first link, environment variables are inherited by the one of the JVM which usually includes PATH. – Guillaume Polet Oct 03 '12 at 20:42
  • @GuillaumePolet But processor builder won't search/expand them. You need to use `System.getenv` and substitute them in the code, or perform the search yourself. I believe there are some exceptions for some of the OS's, but I can never remember them nor do I try to rely on them :P – MadProgrammer Oct 03 '12 at 20:48
  • @ShivKumarGanesh Depends on the IDE. Netbeans uses Ant, which it calls directly via it's API. I wouldn't be surprised if others used the `Tools.jar` directly. – MadProgrammer Oct 03 '12 at 20:50
  • 1
    @MadProgrammer according to [aioobe's answer](http://stackoverflow.com/questions/9368666/where-does-javas-processbuilder-look-to-execute-commands) you should almost never need to perform "PATH-search" and this is what I have experienced so far. – Guillaume Polet Oct 03 '12 at 20:59
  • @GuillaumePolet Not that I don't believe you, but my experience hasn't always been that way (but that could be caused by something else). Do you know if that is a `ProcessBuilder` feature or is actually in the `Process`? I would have thought that the feature would be reliant on the native function in question...I need to do some testing (to satisfy my own reality :P) - I tend to be paranoid about these things and aim for worse case scenario – MadProgrammer Oct 03 '12 at 21:13
  • @GuillaumePolet Thanks for the info. Learn something new every day :D – MadProgrammer Oct 03 '12 at 21:23
0

You can use the Runtime.exec methods to run commands from within Java. The system enviroment variables are normally not visible from within the jvm. You could use a launcher, that submits these system enviroment properties as jvm properties to your java application.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
  • 1
    Or you could use [`System.getenv`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv%28%29) to look up the system environment variables... – MadProgrammer Oct 03 '12 at 20:27
0

According to http://javarevisited.blogspot.de/2011/02/how-to-execute-native-shell-commands.html implementing the following into your code after including "java.lang.Runtime" should perfectly work:

try {

Process process = Runtime.getRuntime().exec("Command to be executed");

} catch (Exception e) {

e.printStackTrace(System.err);

}

FSMaxB
  • 2,280
  • 3
  • 22
  • 41