aspose file tools*
The moose likes Java in General and the fly likes where the code is going wrong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "where the code is going wrong" Watch "where the code is going wrong" New topic
Author

where the code is going wrong

sribu sribu
Greenhorn

Joined: May 02, 2006
Posts: 2
Hi
This program indent is to run linux commands from text field and print the o/p in text area.The program is displaying o/p sometimes and some times it is not.I think it is going wrong in GoodWindowsExec class.Please help me to find the solution for this.
please its urgent...
thanks.

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
class GoodWindowsExec
{
String Exec(String args)
{
String result = "";
try
{
String osName = System.getProperty("os.name" );
System.out.println(" OS NAME is "+osName);
String[] cmd = new String[3];
if( osName.equals( "Linux" ) )
{
cmd[0] = "/bin/bash";
cmd[1] = "-c";
cmd[2] = args;
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]//+"\n");
+ " " + cmd[2]);
Process proc = null;
proc=rt.exec(cmd);
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
outputGobbler.start();
int exitVal =-1;
exitVal=proc.waitFor();
System.out.println("exitVal is:"+exitVal);
result = outputGobbler.text;
System.out.println("Result is:"+result);
}
catch (Throwable t)
{
t.printStackTrace();
}
System.out.println("WIth in result:"+result);
return result;
}
}
public class test2
{
public static void main(String[] args)
{
BoxLayoutFrame frame = new BoxLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// Designing form
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);
}
//Executing commands from text field
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");
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where the code is going wrong
 
Similar Threads
How to free CPU utilised?
run dos cammand in java code
problem calling isql or osql from java
Implementing runAs in Java
how to run batch files from a java program