• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

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.

0
cutie_smily
Asked:
cutie_smily
4 Solutions
 
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
0
 
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
0
 
Tommy BraasCommented:
Have you tried rebooting your machine? It seems that a service on which the 'lpr' command depends is unavailable.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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

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

0
 
Tommy BraasCommented:
The code available in the link I posted is complete.
0
 
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
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now