Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem using the Runtime.exec()

 
Tim Lim
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, the result is still the same.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the same command work fine when you run it on the command line?
 
Tim Lim
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, well, I'm sorry, but I don't know what's wrong.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Lim, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/advanced-java/56978-problem-using-runtime-exec.html
 
Desmond Chua
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic