executing shellscript command from java program

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.

cutie_smilyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rama_krishna580Commented:
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
Tommy BraasCommented:
Have you tried rebooting your machine? It seems that a service on which the 'lpr' command depends is unavailable.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

cutie_smilyAuthor Commented:
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

Tommy BraasCommented:
>> 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
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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
cutie_smilyAuthor Commented:


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.
Tommy BraasCommented:
What apporach did you use? The one I suggested?
cutie_smilyAuthor Commented:

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;

Tommy BraasCommented:
The code available in the link I posted is complete.
cutie_smilyAuthor Commented:

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
Tommy BraasCommented:
>> 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.