LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-22-2009, 02:48 AM   #1
habibur
LQ Newbie
 
Registered: Oct 2009
Location: Kolkata,India
Posts: 1

Rep: Reputation: 0
How can I call a C program in a Shell Script from Java


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.
 
Old 10-22-2009, 09:57 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,098

Rep: Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719
Quote:
Originally Posted by habibur View Post
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)

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.
Your shell script isn't complete. Try:
Code:
#!/bin/bash
mkdir /working/AVClient/habib_test
/full/path/to/NetLibExample 2>&1&
echo "1 image captured"
Try to look up some shell scripting tutorials, which may point you in a better direction.
 
Old 10-23-2009, 04:56 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-24-2009, 03:09 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You might also want to look here:

http://www.javaworld.com/community/node/3585

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
booting to a fullscreen java program with a shell script sebastian.raghe Linux - Newbie 3 05-19-2005 03:42 AM
call a c program in a shell script jagman Programming 4 04-05-2005 05:58 PM
Creating a shell script to run Java program paultaylor Programming 7 11-12-2004 04:11 PM
unix korn shell script that interact with windows java program ann124 Programming 5 11-09-2004 10:14 PM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 08:31 PM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration