Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to execute executable files in linux

843810May 2 2006 — edited Jul 10 2006
Hi
This program displays all system executable commands in the form.This is working fine in windows O.S,but the same program has to be run in linux O.S.Ihave made changes like above but while running it is throwing io exception.
if( osName.equals( "Linux" ) )
{
cmd[0] = " " ;
cmd[1] = "/C" ;
cmd[2] = args;
}
What is the equivalent code in linux operating system for the underlinedcode .


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
String text;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
this.text = "";
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
StringBuffer sb = new StringBuffer();
while ( (line = br.readLine()) != null)
{
sb.append(line);
sb.append("\n");
}
String st = sb.toString();
text = st;
// System.out.println(text);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
class GoodWindowsExec
{
String Exec(String args)
{
String result = "";
String error = "" ;
try
{
String osName = System.getProperty("os.name" );
System.out.println(" OS NAME is "+osName);
String[] cmd = new String[3];

if( osName.equals( "windows 2000" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args;
}

Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
error = errorGobbler.text;
System.out.println("error is:"+error);
result = outputGobbler.text;
// System.out.println("RESULT : \n"+result);
// any error???
// System.out.println("ExitValue: " + exitVal);
}
catch (Throwable t)
{
t.printStackTrace();
}
// return error;
return result;
}
}

public class test2
{
public static void main(String[] args)
{
BoxLayoutFrame frame = new BoxLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}
class BoxLayoutFrame extends JFrame implements ActionListener
{
JLabel enter;
JTextField tf1;
JButton b1;
JTextArea text;
JSplitPane splitPane;
String str;

public BoxLayoutFrame()
{
setTitle("Box Layout");
setSize(500,500);
enter = new JLabel("Enter:");

tf1 = new JTextField(15);
tf1.setMaximumSize(tf1.getPreferredSize());

b1 = new JButton("Go");
b1.addActionListener(this);

Box hbox1 = Box.createHorizontalBox();
hbox1.add(enter);
hbox1.add(Box.createHorizontalStrut(10));
hbox1.add(tf1);
hbox1.add(Box.createGlue());
hbox1.add(Box.createGlue());
hbox1.add(b1);

Box hbox2 = Box.createHorizontalBox();
text = new JTextArea("");
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane jsp = new JScrollPane(text,v,h);
hbox2.add(jsp);

splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,hbox1,hbox2);

Box vbox = Box.createVerticalBox();
vbox.add(splitPane);

add(vbox,BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent evt)
{
try


{
str = tf1.getText();
GoodWindowsExec cmd = new GoodWindowsExec();
text.setText(cmd.Exec(str));

}
catch(Exception exe)
{

text.setText("\t\t"+"file not found:");
text.append("\n");
}
}

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 7 2006
Added on May 2 2006
2 comments
410 views