File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime class

 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I want to have run ./executable command from a Java program.

The problem is the directory where I run my Java program is not in the same directory where the executable file is.The code I have written is shown below. This java program is at the directory Project. And the executable file is at the directory Project/UserProgramData/Shen/Project. Therefore, I set the working directory to be "UserProgramData/Shen/Project". But when I run the program, I got an this execption. :-( Anybody knows what can I do?

java.io.IOException: java.io.IOException: ./executable: not found
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you've got the path right? You can try printing the value of

new File("UserProgramData/Shen/Project/executable").exists()

to see if you've made a mistake.
 
Scott Dunbar
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I'd add that, depending on what the executable is, it may be that you can't find something within the executable. For example, if the executable is a shell script and it is unable to find something like another executable, the error message can somtimes be misleading.

What O/S are you on? In Solaris you can use the truss command and in Linux the trace command to watch what the O/S is trying to do.
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No mistake has been made on the file path.

File executable is on the path "UserProgramData/Shen/Project/" relative to my working directory (the directory of my Java program).

I ran ./executable under "UserProgramData/Shen/Project/" and it works fine there.

For the concurrency concern in my project (distributed system), I have to put the executable file under "UserProgramData/Shen/Project/" and not allowed to be stored on my working directory.

Any ideas about what can I do?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you really test whether the path is right or not -- or are you just "sure" that it's right?
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did tested.



gives me true.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try "executable" instead of "./executable" ?
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File executable is on the path "UserProgramData/Shen/Project/" relative to my working directory (the directory of my Java program).
Okay. And is that directory also the working directory of the process created when you call Runtime.exec()? You seem to be assuming that it is. However since Runtime.exec() has an overloaded method that allows you to specify the created process's working directory, I would say that is a shaky assumption.

Couldn't you just generate the full path of your executable, and pass that to Runtime.exec()?
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-(

The same error was thrown when I use "executable" instead of "./executable".

FYI,

The user program, which is supposed to be executed using ./executable command, takes an inupt.dsp file. I have this file under "UserProgramData/Shen/Project/" as well. Therefore, if I use
runtime.exec("UserProgramData/Shen/Project/executable", null, "UserProgramData/Shen/Project/"), the Java program runs fine. But it couldn't find the input.dsp file and can't generate the output.dsp file.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're in Java 5 look at ProcessBuilder. You can instantiate one and ask it what working directory it thinks it should use. You may find it's not the one you expect. ProcessBuilder has other neat features, too.
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same thing. I can't even open an emacs in other directories?
This is the code I wrote to test:



The output from the shell is:



Is there anything wrong with my code?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've actually got a file named "emacs executable" (with a space in the name) and it's on your path?
[ November 02, 2005: Message edited by: Ernest Friedman-Hill ]
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. It is generated using the gcc linker commnad. Executing ./executable takes an input.dsp file and generates an output.dsp file if I manually do it under the directory where executable file is.
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"emacs executable" as a whole is the command to open executable file using emacs editor.

executable is actually a file generated using gcc linker command
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a faq on the net years ago about executing runnables.

Have you tried using a shell to execute that command? Im not sure what shell you get be default when you try to run something in your setup but maybe you need to

'sh ./executable'

so to speak
 
Lin Shen
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is solved.

I put:

cd dir (dir is the file path to where executable and my input file stores)
./executable

in a script exec.sh and then call the exec method in the Runtime class
to execute the exec.sh script like this: runtime.exec("exec.sh");

And the problem has been solved. Thanks for people's help.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic