Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I execute shell command using a java gui? 1

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
US
I am new to java and am building a java gui in 1.3.1 in AIX 5.1. I am trying to figure out how to get the gui to execute korn shell commands?

Thanks in advance.
Jeremy
 
I believe you have to create a new process and then get the current runtime and have that runtime execute the shell command. To make this GUI, just add the code in a listener to the compotent that is causing this to happen.

Hope that Helps
JavaDude32
 
You have to use Runtime.exec(). Realize that this is entirely non-portable and somewhat defeats the purpose of using java.

For instance, to display a directory listing to the console, you would use

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("ls");
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
 
how would i get it to interact with an existing shell program?

Thanks again in advance.

Jeremy
 
I also want to be able to do things in the background, and have it display what the shell is doing to the java window.

Any help would be wonderful.

Jeremy
 
You can't get it to interact with a current shell window. YOu can only spawn a new one and interact with it's output.
 
I tried adding that bit of code in, and it didnt work??

Any suggestions?
 
here is the program i wrote, and the errors i got. Please help.


import javax.swing.*; //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
//Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;

public class javatry {


Runtime rt = Runtime.getRuntime();
Process p = rt.exec("ls");
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
}




javatry.java:15: illegal start of type
while ((input = reader.readLine()) != null)
^
javatry.java:18: <identifier> expected
}
^
javatry.java:13: cannot resolve symbol
symbol : class BufferedReader
location: class javatry
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in));
^
javatry.java:13: cannot resolve symbol
symbol : class InputStreamReader
location: class javatry
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in));
^
4 errors
 
You need to import java.io.BufferedReader or java.io.*;

You also need to put the code into a method such as main.
 
i am still having troubles...here is the new code with the errors.

import javax.swing.*; //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
//Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class javatry {
public void main(String[] args) {
catch (Exception e)
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(&quot;ls&quot;);
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in
));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
}
}



javatry.java:9: 'catch' without 'try'
catch (Exception e)
^
javatry.java:11: cannot resolve symbol
symbol : variable rt
location: class javatry
Process p = rt.exec(&quot;ls&quot;);
^
2 errors



 
The code should look like this:

import javax.swing.*; //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
//Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class javatry {
public void main(String[] args) throws Exception {

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(&quot;ls&quot;);
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(in
));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
}
}
 
it still gives me these errors; here again is the code and errors.

import javax.swing.*; //This is the final packag


import javax.swing.*; //This is the final packag
e name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 a
nd all
//Swing releases before Sw
ing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class javatry2 {
public void main(String[] args) throws Exception {

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(&quot;ls&quot;);
java.io.InputStream in = p.getInputStream();
java.io.BufferedReader reader = new BufferedReader(new
InputStreamReader(in
));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
}
}


javatry.java:9: 'catch' without 'try'
catch (Exception e)
^
javatry.java:11: cannot resolve symbol
symbol : variable rt
location: class javatry
Process p = rt.exec(&quot;ls&quot;);
^
2 errors


 
This compiles and runs fine. If it doesn't work for you, you are doing something wrong.

import java.io.*;
public class Test
{

public void main(String[] args) throws Exception {

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(&quot;ls&quot;);
InputStream in = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String input;
while ((input = reader.readLine()) != null)
{
System.out.println(input);
}
}
}
 
thank you. that compiled correctly, but upon executing it, it gives me this error:

Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: main


any more guesses??
thanks a ton,

Jeremy
 
What the heck. It ran fine for me.
In any event, the correct method signature should be:

public static void main (String args []) throws Exception
 
:) great! thanks so much, that worked. now if i want to get it to execute something, i can just replace the &quot;ls&quot; with &quot;./testprogram&quot; right??

J
 
Yep.

I gave you a simple example. You can programmatically read output from and send input to your program if need be while it is running. For more information, check out the JDK api at java.sun.com


Regards,

Charles
 
Thats great way of explaining things with patience meadandale.Even a fresher can get the topic easily.

well good job done

srinu
 
It works and to run a DOS command using a Java program u can use the following piece of code..

Runtime rt=Runtime.getRuntime();
Process p=rt.exec(&quot;cmd /c dir&quot;); // For NT
Process p=rt.exec(&quot;C:/command dir&quot;); // For 98
p.waitFor();
 
how about if you want to print out errors to standard output? what does the waitFor() method do? what does the join() method do?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top