Having trouble starting a program with the Runtime.getRuntime().exec(command) - java

I'm currently writing a Java program that can open .exe programs on my PC, like MS Word for example.
I am having a problem though, because Runtime.getRuntime().exec() will only successfully open certain programs. I have used the exact same code for all the programs, but regardless, some programs won't open.
Here is my code for a program I downloaded, Picasa 3:
class picasaHandler implements ActionListener
{
public void actionPerformed(ActionEvent r)
{
try
{
Runtime.getRuntime().exec("cmd /c start Picasa3.exe");
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find Picasa 3");
}
}
}
So my question is, why won't Runtime.getRuntime().exec() run all the programs I use it on, and how do I run programs like Picasa 3, that I cannot run at this moment with this method.

I'm guessing that Picasa3.exe is not on your %PATH% anywhere so it doesn't know how to load it. Have you tried specifying the full path to Picasa3.exe?
Runtime.getRuntime().exec("cmd /c \"c:\\program files (x86)\\Google\\Picasa3\\Picasa3.exe\"")

File file=new File("picassa3");
String filename=file.getAbsolutePath(file);
try
{
Runtime.getRuntime().exec(filename);
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find the file");
}

Runtime's exec can only start applications that are on the Windows path. Some programs are automatically on the path, while others, like Picasa, is not.
The only work-around for this is to determine the correct path and then launch that application.

This might work for you.
If you want to run a certain program using Runtime.exec(), just add it's installation path to path variable in your System Variables. To find it's installation path, simply right click on it's shortcut and select "Find Target". Then concat that entire address at the end of your path Variable.

Related

Using rsync4j library, a Java implementation of Linux rsync tool, how to sync data from a remote directory with a local directory

I need the functionality like that of the rsync linux tool in my Java program. For that, I chose the rsync4j library.
Using their documentation, I wrote the following program:
import com.github.fracpete.processoutput4j.output.ConsoleOutputProcessOutput;
import com.github.fracpete.rsync4j.RSync;
public class MainClass {
public static void main(String [] args) {
System.out.println("Started");//check
RSync rsync = new RSync()
.source("/home/arth/DataSourceFolder/a.txt")
.destination("/home/arth/DataDestinationFolder/")
.recursive(true);
// or if you prefer using commandline options:
// rsync.setOptions(new String[]{"-r", "/one/place/", "/other/place/"});
CollectingProcessOutput output = null;
try {
System.out.println("Inside try");
output = rsync.execute();
System.out.println("End of try");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(output.getStdOut());
System.out.println("Exit code: " + output.getExitCode());
if (output.getExitCode() > 0)
System.err.println(output.getStdErr());
}
}
In the snippet, in out local machine, a file a.txt is copied from one location to another. This works perfectly. The file is successfully copied when I run it and here is the output:
Started
Inside try
End of try
Exit code: 0
But my need is to sync a local directory with a directory lying at a remote host/machine. When I tried to do it using a simple rsync command from a terminal using the following command
rsync remoteUserName#23.24.25.244:/home/beth/remoteFolder/a.png /home/arth/DataSourceFolder
it works like a charm. a.png IS copied to local machine at path specified, although a password of remote machine is asked first.
But the problem when I use the above Java program to do the same operation, by replacing line # 11 and 12 by:
.source("remoteUserName#23.24.25.244:/home/beth/remoteFolder/a.png")
.destination("/home/arth/DataDestinationFolder/")
the program gets stuck after printing Started in the console. Neither an exception is thrown nor does the program proceed.
The question is that how do I fix this problem?
(old post, I know, but here it goes...) The rsync4j library does not allow interaction. In your case, the underlying rysnc binary prompts for a password in the process that the Java library created, but never receives one.
Starting with release 3.2.3-7, you can supply an instance of the sshpass wrapper to feed in the password (see this comment for an example).

Java need process to return when finished

my problem would take 2 questions, but I'll keep it short. So I need to launch a bat file. Right now I do it like this:
public static void check() throws InterruptedException{
try {
Runtime.getRuntime().exec("cmd /c start build.bat");
Thread.sleep(3000);
} catch (IOException e) {
e.printStackTrace();
}
}
The bat file launches the java compiler to compile another java file and direct the error messages into a txt file. This is what the bat file looks like:
#echo off
javac -Xstdout error.txt MainApp.java
exit
Now the problem is, that I have to include a 3 second sleep, in order to be sure, that the error.txt has been created and filled with errors. This is very unsatisfying. I'd either need a return value from the bat file, so I the rest of the program waits, until it's done or a way to launch the java compiler out of the program and direct the error messages into a txt file.
Thanks everybody.
You can use Process#waitFor:
Causes the current thread to wait, if necessary, until the process
represented by this Process object has terminated
Process p = Runtime.getRuntime().exec("cmd /c start build.bat");
p.waitFor();

How to launch an EXE as an argument in java, but still with parameters

I'm in a situation where I need to launch a .exe, but I meed to launch it with parameters, AND it needs to be launched as an argument itself, but cant be launched from a command line. I need to launch javaw itself, with my exe as an argument. For example:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "PATH/TO/.exe"
However, "PATH/TO/.exe" needs -arg1 and -arg2. Placing the parameters inside of the path leads to java not finding them, whereas placing them outside leads to java thinking they belong to itself and trying to use them.
EDIT: I am running the exe with java because I am using a completely different program to start all this. I cannot use a different java program, all I have to work with is the target box. It is designed to start exe's, but I need to run the exe through java. ALL I have to use is the path area where you would usually type where the exe is located.
The basic trick is:
try {
final Process p = Runtime.getRuntime().exec(ARRAY_OF_EXE_AND_ARGUMENTS);
if (p == null) {
// some error
// probably command not found, abort.
}
try {
final int retval = p.exitValue();
if (retval == 0) {
// Process ended immediately.
// Probably a problem, abort.
} else {
// Process crashed, abort.
}
} catch (final IllegalThreadStateException itse) {
// Process is running.
// all is good
}
} catch (final IOException e) {
// Error running exe, abort.
}
Where ARRAY_OF_EXE_AND_ARGUMENTS is a String array, where first is path to the executable, and following are it's command-line arguments.
To pass the exe and argumetns to java, this is the common pattern:
java -jar MyJar.jar "/path/to/virus.exe argument1 argument2"
And then you split it by space or something.

How to create a process which can execute concurrently with respect to java process [duplicate]

I am working on a program written in Java which, for some actions, launches external programs using user-configured command lines. Currently it uses Runtime.exec() and does not retain the Process reference (the launched programs are either a text editor or archive utility, so no need for the system in/out/err streams).
There is a minor problem with this though, in that when the Java program exits, it doesn't really quit until all the launched programs are exited.
I would greatly prefer it if the launched programs were completely independent of the JVM which launched them.
The target operating system is multiple, with Windows, Linux and Mac being the minimum, but any GUI system with a JVM is really what is desired (hence the user configurability of the actual command lines).
Does anyone know how to make the launched program execute completely independently of the JVM?
Edit in response to a comment
The launch code is as follows. The code may launch an editor positioned at a specific line and column, or it may launch an archive viewer. Quoted values in the configured command line are treated as ECMA-262 encoded, and are decoded and the quotes stripped to form the desired exec parameter.
The launch occurs on the EDT.
static Throwable launch(String cmd, File fil, int lin, int col) throws Throwable {
String frs[][]={
{ "$FILE$" ,fil.getAbsolutePath().replace('\\','/') },
{ "$LINE$" ,(lin>0 ? Integer.toString(lin) : "") },
{ "$COLUMN$",(col>0 ? Integer.toString(col) : "") },
};
String[] arr; // array of parsed tokens (exec(cmd) does not handle quoted values)
cmd=TextUtil.replace(cmd,frs,true,"$$","$");
arr=(String[])ArrayUtil.removeNulls(TextUtil.stringComponents(cmd,' ',-1,true,true,true));
for(int xa=0; xa<arr.length; xa++) {
if(TextUtil.isQuoted(arr[xa],true)) {
arr[xa]=TextDecode.ecma262(TextUtil.stripQuotes(arr[xa]));
}
}
log.println("Launching: "+cmd);
Runtime.getRuntime().exec(arr);
return null;
}
This appears to be happening only when the program is launched from my IDE. I am closing this question since the problem exists only in my development environment; it is not a problem in production. From the test program in one of the answers, and further testing I have conducted I am satisfied that it is not a problem that will be seen by any user of the program on any platform.
There is a parent child relation between your processes and you have to break that.
For Windows you can try:
Runtime.getRuntime().exec("cmd /c start editor.exe");
For Linux the process seem to run detached anyway, no nohup necessary.
I tried it with gvim, midori and acroread.
import java.io.IOException;
public class Exec {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("/usr/bin/acroread");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}
I think it is not possible to to it with Runtime.exec in a platform independent way.
for POSIX-Compatible system:
Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "your command"}).waitFor();
I have some observations that may help other people facing similar issue.
When you use Runtime.getRuntime().exec() and then you ignore the java.lang.Process handle you get back (like in the code from original poster), there is a chance that the launched process may hang.
I have faced this issue in Windows environment and traced the problem to the stdout and stderr streams. If the launched application is writing to these streams, and the buffer for these stream fills up then the launched application may appear to hang when it tries to write to the streams. The solutions are:
Capture the Process handle and empty out the streams continually - but if you want to terminate the java application right after launching the process then this is not a feasible solution
Execute the process call as cmd /c <<process>> (this is only for Windows environment).
Suffix the process command and redirect the stdout and stderr streams to nul using 'command > nul 2>&1'
It may help if you post a test section of minimal code needed to reproduce the problem. I tested the following code on Windows and a Linux system.
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec(args[0]);
}
}
And tested with the following on Linux:
java -jar JustForTesting.jar /home/monceaux/Desktop/__TMP/test.sh
where test.sh looks like:
#!/bin/bash
ping -i 20 localhost
as well as this on Linux:
java -jar JustForTesting.jar gedit
And tested this on Windows:
java -jar JustForTesting.jar notepad.exe
All of these launched their intended programs, but the Java application had no problems exiting. I have the following versions of Sun's JVM as reported by java -version :
Windows: 1.6.0_13-b03
Linux: 1.6.0_10-b33
I have not had a chance to test on my Mac yet. Perhaps there is some interaction occuring with other code in your project that may not be clear. You may want to try this test app and see what the results are.
You want to launch the program in the background, and separate it from the parent. I'd consider nohup(1).
I suspect this would require a actual process fork. Basically, the C equivalent of what you want is:
pid_t id = fork();
if(id == 0)
system(command_line);
The problem is you can't do a fork() in pure Java. What I would do is:
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
Runtime.getRuntime().exec(command);
}
catch(IOException e)
{
// Handle error.
e.printStackTrace();
}
}
});
t.start();
That way the JVM still won't exit, but no GUI and only a limited memory footprint will remain.
I tried everything mentioned here but without success. Main parent Java process can't quit until the quit of subthread even with cmd /c start and redirecting streams tu nul.
Only one reliable solution for me is this:
try {
Runtime.getRuntime().exec("psexec -i cmd /c start cmd.cmd");
}
catch (Exception e) {
// handle it
}
I know that this is not clear, but this small utility from SysInternals is very helpful and proven. Here is the link.
One way I can think of is to use Runtime.addShutdownHook to register a thread that kills off all the processes (you'd need to retain the process objects somewhere of course).
The shutdown hook is only called when the JVM exits so it should work fine.
A little bit of a hack but effective.

Couldn't run bat file through Java code

Problem is that the same code below is working on other machine with Windows 7. I also use Windows 7, and bat file works well. But if I try to run this bat from code written before, cmd window just blink once and disappear.
s = path + "makeInfomap.bat";
try {
p = run.exec(s);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
final int exitVal = p.waitFor();
Run don't walk to this link: When Runtime.exec() won't.
It will tell you how to gobble output and error streams and will tell you how to call the OS's command interpreter when doing similar programs (although it is a little out of date).
My guess is that java is calling it fine but that the batch file itself is running in to trouble.
Try adding a 'pause' as the last line of your batch file and see if the batch file's console gives you any usable information.

Categories