aspose file tools*
The moose likes Java in General and the fly likes Runtime class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime class" Watch "Runtime class" New topic
Author

Runtime class

Lin Shen
Ranch Hand

Joined: Oct 08, 2003
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
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.


<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Lin Shen
Ranch Hand

Joined: Oct 08, 2003
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Did you really test whether the path is right or not -- or are you just "sure" that it's right?
Lin Shen
Ranch Hand

Joined: Oct 08, 2003
Posts: 57
I did tested.



gives me true.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Did you try "executable" instead of "./executable" ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

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

Joined: Oct 08, 2003
Posts: 57
:-(

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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lin Shen
Ranch Hand

Joined: Oct 08, 2003
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 08, 2003
Posts: 57
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

Joined: Oct 08, 2003
Posts: 57
"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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Oct 08, 2003
Posts: 57
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime class