The Source for Java Technology Collaboration

Home » java.net Forums » JDK » JDK Open Source

Thread: How can I call a C program in a Shell Script from Java

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is not answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 1 - Last Post: Oct 22, 2009 5:13 AM by: jesdisciple Threads: [ Previous | Next ]
habiburr

Posts: 1
How can I call a C program in a Shell Script from Java
Posted: Oct 22, 2009 1:47 AM
 
  Click to reply to this thread Reply

Hi all,
I have a C program namely NetLibExample.c.I have compiled it to get a NetLibexample executable in RHEL5. It is successfully running from the terminal itself by the syntax: ./NetLibExample
I have written a shell script to call the same.The shell script is test.sh

test.sh
cd /working/AVClient
mkdir habib_test
./NetLibExample
echo "1 image captured"

This shell is running successfully and giving me the output( a new folder habib_test and output from the C program)

I have to call the shell script from Java program.The program is like this.
TestShell.java
public class TestShell {
public static void main(String[] args) {
try{
System.out.println("I am here to test the shell correctly");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("/working/test.sh");
int exitVal = proc.waitFor();

}catch(Exception e){
e.printStackTrace();
System.out.println("Exception caught.........."+e.toString());
}
}
}


But when I am trying to call the shell from Java,shell is runnin g properly as I am getting the new folder created from the shell.The java program is giving a exception:
java.lang.IllegalThreadStateException: process hasn't exited
at java.lang.UNIXProcess.exitValue(UNIXProcess.java:172)
at TestShell.main(TestShell.java:27)
Exception caught............java.lang.IllegalThreadStateException: process hasn't exited

Please help me.Thanks in advance.

jesdisciple

Posts: 21
Re: How can I call a C program in a Shell Script from Java
Posted: Oct 22, 2009 5:13 AM   in response to: habiburr
 
  Click to reply to this thread Reply

Hi habiburr. First off, note that I haven't programmed in Java since a long time. I'm here as a Linux geek to nurture this forum. If you have similar interest in the project, I'd appreciate your help - answering questions and petitioning the site for a moderator.

As to your question, which line is #27? If you can identify that, I might be able to help. Although I won't be working off my own knowledge in this area because it doesn't exist; I never went this far with Java and will have to rely on Google.




 XML java.net RSS