?
Solved

executing shellscript command from java program

Posted on 2006-04-18
15
Medium Priority
?
441 Views
Last Modified: 2008-02-01
Below is the program java program

import java.io.*;

import java.lang.*;
public class ShellTest {

    public static void main(String args[]) throws IOException, InterruptedException {
         Runtime runtime = Runtime.getRuntime();
         String command = "printing.sh";
         try{
            Process process = runtime.exec("test.sh");
            process.waitFor(); // block until completion

            } catch(IOException e) {
                System.out.println(e);
        }

         return;
   }
}

test.sh
------

lpr test1.txt


Printer is not connected to my linux machine. But the java program is not throwing any message to console. It used out message  

lpr: error - scheduler not responding!

The above error is not displaying from java prog. does it when executes .sh file. Can anyone figure out. How to print from a file from a java program.

0
Comment
Question by:cutie_smily
12 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 500 total points
ID: 16480441
You could use the printing mechanism in java itself. Some background on that:

http://java.sun.com/j2se/1.4.2/docs/guide/jps/index.html
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 500 total points
ID: 16480547
Hi,

try this,....

public void runCmd()
{
Runtime r = Runtime.getRuntime(); //get runtime information
try
{

Process Child = r.exec("/usr/bin/ksh") ; //execute command
BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(Child.getOutputStream()));
outCommand.write("/TestPath/TestScript.sh");
outCommand.flush();
try
{
Child.waitFor(); //wait for command to complete
}
catch(InterruptedException e)
{ //handle waitFor failure
System.out.println("ERROR: waitFor failure");
System.exit(10); //exit application with exit code 10
}
}
catch(IOException e)
{ //handle exec failure
System.out.println("ERROR: exec failure"+e);
System.exit(11); //exit application with exit code 11
}
}

R.K
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16480614
Have you tried rebooting your machine? It seems that a service on which the 'lpr' command depends is unavailable.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cutie_smily
ID: 16480970
Mr. Rama Krishna, can i assign

Process Child = r.exec("/usr/bin/ksh") ; //execute command
outCommand.write("/TestPath/TestScript.sh");


can i assign like
Process Child = r.exec("/tmp/test.sh") ; //execute command
outCommand.write("/tmp/TestScript.sh");

Orangehead, when i write lpr filename on console ..it displays
lpr: error - scheduler not responding!
which means no printer attached or unavilable.

But i need above message on console when i execute my java program

0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 500 total points
ID: 16481112
>> But i need above message on console when i execute my java program
I see. That's easy.

See the following question for guidance:  http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20979943.html?query=output+from+process&clearTAFilter=true
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 500 total points
ID: 16481142
http://forum.java.sun.com/thread.jspa?threadID=293692&start=0

http://www.java2s.com/Code/Java/2D-Graphics-GUI/Print.htm

check out.

String[] cmd = {"/usr/bin/ksh", "/test.sh"};

Process p = Runtime.getRuntime().exec(cmd);
 process.waitFor();

try the above snipplet.

else check

http://www.jguru.com/forums/view.jsp?EID=1225863
0
 

Author Comment

by:cutie_smily
ID: 16484144


What i observe is that Process.exec(cmd) not getting executed. Even i insert wrong commands. Its not throwing an error. How to resolve above.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16484641
What apporach did you use? The one I suggested?
0
 

Author Comment

by:cutie_smily
ID: 16490756

I am using shivaspk / ramakrishna's logic.

I see the the problem is Process.exec statement not getting executed.

I did this

Process p=Runtime.getRuntime().exec("echo Radha");

I don't see any result. I also tried giving

Runtime.getRuntime().exec("pr filename.txt);  --not throwing an error and i don't see anything on the console.

I am using redhat 9 and i saved my file in /tmp folder.

What could be the problem. Why such a small thing taking this long.

Anyway, if anyone can figures out let me know;

0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16490879
The code available in the link I posted is complete.
0
 

Author Comment

by:cutie_smily
ID: 16499107

It looks complicated and also it looks like it works on windows.

The above code is so simple and small. For some reason its not working. Can any experts help me out. oragehead, I will look at the link in detail and understand. Thankyou
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16500912
>> The above code is so simple and small.
It is like that because it is incomplete, and because it is incomplete it is not working.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question