• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Runtime class

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did tested.



gives me true.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try "executable" instead of "./executable" ?
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic