LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices

Reply
 
Search this Thread
Old 10-25-2011, 03:50 AM   #1
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Rep: Reputation: 0
Running shell script in Ubuntu from a java program


Hi,

I am developing a Java program where I need to execute multiple lines of code from the terminal. It also includes some internal commands like cd, so, I thought it would be a good idea to use shell script. But, I get some error while executing shell script from the java program.

Also, I am new to shell script programming. Can anyone tell me how to run a shell script from a java program with a simple example?

Thanks,
Ashish.
 
Old 10-25-2011, 05:21 AM   #2
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
http://www.jguru.com/faq/view.jsp?EID=344255
 
Old 10-25-2011, 10:16 AM   #3
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi again,

I tried passing the command by running a shell but cd is not executed. It gives error 'cannot open cd (path)'.
How can I run internal commands like cd from my java program?

Thanks,
Ashish.
 
Old 10-25-2011, 10:52 AM   #4
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
cd is a built in command, not a command by itself. At least in bash.
you should try creating your own script, and call that from java.
Your script can then use cd if it's a bash script (#!/bin/bash)
 
Old 10-28-2011, 05:47 AM   #5
ashish.ars@gmail.com
LQ Newbie
 
Registered: Oct 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi once again,

I am facing some problem in running shell script from a java program. The script runs fine when executed from the terminal. But, when executed from the java program, not whole script is executed. My java code to implement shell script execution is:

File file = new File("/path/to/script");
String COMMAND= "./run";
ProcessBuilder p = new ProcessBuilder(COMMAND);
p.directory(file);

try {
Process start= p.start();

} catch (IOException e) {

e.printStackTrace();
}

It seems only the first line is executed. "run" is the script name.

Any suggestions?

Thanks,
Ashish.

Last edited by ashish.ars@gmail.com; 10-28-2011 at 05:49 AM.
 
  


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
How can I call a C program in a Shell Script from Java habibur Programming 3 10-24-2009 02:09 AM
running a shell script which invokes another shell program!!! raghu123 Programming 1 08-04-2008 09:10 AM
Running shell script within a C or C++ program Quantum0726 Programming 2 06-15-2005 09:14 PM
running SHell script from java anirudh Programming 2 01-07-2005 07:37 AM
Running Java Socket Program in a shell during bootup TomCruise2002 Linux - Newbie 1 10-15-2002 10:28 PM


All times are GMT -5. The time now is 07:10 PM.

Main Menu
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