wood burning stoves 2.0*
The moose likes Java in General and the fly likes problem using the Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem using the Runtime.exec()" Watch "problem using the Runtime.exec()" New topic
Author

problem using the Runtime.exec()

Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
Hi all, I have some problems regarding the use of Process class.

I have an external application that I need to call using Process. The external application that I use is BulkImageProcessor which can only use command line parameters to call the functions of the program.

In my code, I wrote something like this.



The error I got from console is that



So what went wrong?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Instead of "exec(String)", use the "exec(String[])" method, that's easier to get right. Also read this article for all the things that can go wrong with Runtime.exec.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
Nope, the result is still the same.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Does the same command work fine when you run it on the command line?
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
the same command will work fine if I will to run it in command prompt. I dont understand how it does not work in this case. It just complains there is a missing library which is the PDNBulkUpdater.dll but actually the library resides in the Paint.Net/Effects directory
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tim Lim wrote:the same command will work fine if I will to run it in command prompt. I dont understand how it does not work in this case. It just complains there is a missing library which is the PDNBulkUpdater.dll but actually the library resides in the Paint.Net/Effects directory


Your environment when executing from within Java is not the same as when executing on the command line. It's nigh impossible to find the exact problem without being on your system, but a few things to look for would be:

1) PATH : I would expect this is probably the same, but you may want to compare it just to be sure.

2) In Linux we usually use LD_LIBRARY_PATH or something like that for finding .sos (like .dlls). I don't recall if Windows has a similar thing, or if it just uses PATH. (If it does use a separate variable, rather than PATH, then #1 is kind of pointless.)

3) If PATH or whatever it uses to find DLLs has any relative paths on it, then for that to work, you'll have to make sure your Java app has the same current working directory as when you run the command in the console. Since pwd is not something we usually control or care about in a Java program, it's better to just use absolute paths.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
@Jeff Verdegan

For 1) I try to use Java to print out the PATH. So far what I do is to use System.getProperty(java.library.path) to print out all the path. So far it is the same as the one in my environment variable

For 3) I have tried to add the DLL into the PATH but still it does not work. It still prints out the same error message that the DLL is not found. Regarding to have the Java app to put the same current working directory, can I ask you mean I have to put the app in the same directory as the Paint.Net? If it is so, is there any other way to make it work other than putting it in the same working directory? I actually intended to make it such a way that the java app can be worked regardless of where the app is placed in which directory.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tim Lim wrote:@Jeff Verdegan

For 1) I try to use Java to print out the PATH. So far what I do is to use System.getProperty(java.library.path) to print out all the path. So far it is the same as the one in my environment variable


Are there any relative paths in it? Or are they all absolute?

Did you find the one that contains the DLL in question?


For 3) I have tried to add the DLL into the PATH but still it does not work. It still prints out the same error message that the DLL is not found. Regarding to have the Java app to put the same current working directory, can I ask you mean I have to put the app in the same directory as the Paint.Net? If it is so, is there any other way to make it work other than putting it in the same working directory? I actually intended to make it such a way that the java app can be worked regardless of where the app is placed in which directory.


No, I didn't mean put the app in the same directory as Paint.Net. I mean that your app shouldn't rely on being run from any particular directory, so if it's using paths on the file system, they should be absolute paths, not relative.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
Jeff Verdegan wrote:
Tim Lim wrote:@Jeff Verdegan

For 1) I try to use Java to print out the PATH. So far what I do is to use System.getProperty(java.library.path) to print out all the path. So far it is the same as the one in my environment variable


Are there any relative paths in it? Or are they all absolute?
They are absolute path

Did you find the one that contains the DLL in question?
Yup, they did contain the folder with the DLL.

Still the error is the same.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okay, well, I'm sorry, but I don't know what's wrong.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4547
    
    5

Tim Lim, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/advanced-java/56978-problem-using-runtime-exec.html


luck, db
There are no new questions, but there may be new answers.
Desmond Chua
Greenhorn

Joined: Mar 20, 2012
Posts: 1
The crucial point is that your program needs a working directory to fall on before you can run it. And that lies at line 8

Process p = rt.exec(command);

This is the particular overload method which fits your case.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java.io.File%29

The 2nd param environment variable may not be required; you can set it to null. Thus you can write this instead.

Process p = rt.exec(command, null, new File("C:\\Paint.NET"));

Hope it strikes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem using the Runtime.exec()