Mailinglist Archive: opensuse-programming (128 mails)
| < Previous | Next > |
Re: [suse-programming-e] how to call linux command with java ?
- From: dries <dries@xxxxxxxxxxxxx>
- Date: Wed, 16 Apr 2003 16:46:46 -0700
- Message-id: <200304161646.46489.dries@xxxxxxxxxxxxx>
Linux Aremania wrote :
| I wanna make java application which call linux
| command, but I don't know how to call linux command.
| For example : I wanna display the output of "ps -ax"
| command in applet.
|
you can't do that from an applet, protected !!, what you can do is write a
java program that does this. But your browser won't allow you to execute
external commands from an applet
in a java program, this class can help you :
//
// (C) Dries Pruimboom (dries@xxxxxxxxxxxxx)
// Use at own risk !!
// Might not be fit for your use, you decide !!
//
//
// Executer class for simple execution of external programs
//
// General usage :
//
// Executer exe = new Executer("find /");
// String line;
//
// while ((line = exe.ReadLine())!=null)
// {
// System.out.println(line);
// }
//
class Executer
{
Process proc;
BufferedReader input;
BufferedWriter output;
public int ExitVal;
//
// str contains the command to execute
//
Executer(String str)
{
try
{
Runtime runtime = Runtime.getRuntime();
proc = runtime.exec(str);
input = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
output = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
}
catch (Exception e)
{
System.out.println(e);
}
}
//
// Read a line of output from external program
//
public String ReadLine()
{
String hlp=null;
try {
hlp = input.readLine();
}
catch (Exception e)
{
hlp=null;
}
return (hlp);
}
//
// Write line of data to external program
//
public void WriteLine(String str)
{
try
{
output.write(str);
output.flush();
}
catch (Exception e)
{
}
}
public boolean DataAvail()
{
boolean res=false;
try
{
res = input.ready();
}
catch (IOException e)
{
}
return(res);
}
//
// Do a more or less nice end of this process
// this is not actually needed!.
//
public void EndProcess()
{
try {
input.close();
output.close();
}
catch (Exception e)
{
}
}
public boolean Stopped()
{
boolean res = false;
if (!DataAvail())
{
try {
ExitVal = proc.exitValue();
res=true;
}
catch (IllegalThreadStateException e)
{
}
}
return(res);
}
}
Hope this will help you
Grtz Dries
--
<End of message>
| I wanna make java application which call linux
| command, but I don't know how to call linux command.
| For example : I wanna display the output of "ps -ax"
| command in applet.
|
you can't do that from an applet, protected !!, what you can do is write a
java program that does this. But your browser won't allow you to execute
external commands from an applet
in a java program, this class can help you :
//
// (C) Dries Pruimboom (dries@xxxxxxxxxxxxx)
// Use at own risk !!
// Might not be fit for your use, you decide !!
//
//
// Executer class for simple execution of external programs
//
// General usage :
//
// Executer exe = new Executer("find /");
// String line;
//
// while ((line = exe.ReadLine())!=null)
// {
// System.out.println(line);
// }
//
class Executer
{
Process proc;
BufferedReader input;
BufferedWriter output;
public int ExitVal;
//
// str contains the command to execute
//
Executer(String str)
{
try
{
Runtime runtime = Runtime.getRuntime();
proc = runtime.exec(str);
input = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
output = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
}
catch (Exception e)
{
System.out.println(e);
}
}
//
// Read a line of output from external program
//
public String ReadLine()
{
String hlp=null;
try {
hlp = input.readLine();
}
catch (Exception e)
{
hlp=null;
}
return (hlp);
}
//
// Write line of data to external program
//
public void WriteLine(String str)
{
try
{
output.write(str);
output.flush();
}
catch (Exception e)
{
}
}
public boolean DataAvail()
{
boolean res=false;
try
{
res = input.ready();
}
catch (IOException e)
{
}
return(res);
}
//
// Do a more or less nice end of this process
// this is not actually needed!.
//
public void EndProcess()
{
try {
input.close();
output.close();
}
catch (Exception e)
{
}
}
public boolean Stopped()
{
boolean res = false;
if (!DataAvail())
{
try {
ExitVal = proc.exitValue();
res=true;
}
catch (IllegalThreadStateException e)
{
}
}
return(res);
}
}
Hope this will help you
Grtz Dries
--
<End of message>
| < Previous | Next > |