How to call linux command ?

hi all,
I wanna make java application which display the ouput of linux command (eg. ps -ax output in JTextArea).
The problem is how to call "ps,kill,etc" ?

JTextArea txtOutput = new JTextArea();
String cmd = "ps -a";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String out;
while ((out = buf.readLine()) != null){
System.out.println("Output: "+out);
txtOutput.append(out+"\n");
}

Similar Messages

  • How to call gnuplot command from java

    Hi there,
    In our course, we are required to develop an GUI for gnuplot. In case you don't know about gnuplot, it's a plotting program and has lots of command. We want to use java and swing, but now we don't know how to call gnuplot command from java, or how to execute a shell command(script) from java.
    By the way, since we need read in files with several columns of data and allow user to select a column, we want to use JTable. Is that reasonable?
    Thanks a lot for any suggestions!
    Jack

    Hi, there:
    Will using JTable add much overhead? I may have to use several JTables and switch among them. I can add scroll bar to or edit JTables, right?
    BTW, do you have experience about gnuplot? Can I find the command tree of gnuplot somewhere? Or do you know a better place to post question about gnuplot? unix/linux group, maybe.
    Thanks,
    Jack
    P.S. Would you guys answer my question after I use up my duke dollars? :- )

  • How to execute Linux command from Java app.

    Hi all,
    Could anyone show me how to execute Linux command from Java app. For example, I have the need to execute the "ls" command from my Java app (which is running on the Linux machine), how should I write the codes?
    Thanks a lot,

    You can use "built-in" shell commands, you just need to invoke the shell and tell it to run the command. See the -c switch in the man page for your shell. But, "ls" isn't built-in anyays.
    If you use exec, you will want to set the directory with the dir argument to exec, or add it to the command or cmdarray. See the API for the variants of java.lang.Runtime.exec(). (If you're invoking it repeatedly, you can most likely modify a cmdarray more efficiently than having exec() decompose your command).
    You will also definitely want to save the returned Process and read the output from it (possibly stderr too and get an exit status). See API for java.lang.Process. Here's an example
    java.io.BufferedReader br =
    new java.io.BufferedReader(new java.io.InputStreamReader(
    Runtime.getRuntime().exec ("/sbin/ifconfig ppp0").
    getInputStream()));
    while ((s = br.readLine()) != null) {...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Calling linux command line in Java

    Hi guys,
    I met a problem about calling linux comand in java program.
    In linux, I type :
    similarity.pl -type WordNet::Similarity::vector -file /home/csunix/fzsu/exam.txt
    it returns:
    Loading WordNet... done.
    Loading Module... done.
    good#a#1 nice#a#1
    good#a#1 nice#a#1 0.885813537770033
    dog#n#1 cat#n#1
    dog#n#1 cat#n#1 0.712603873475428
    good#a#6 nice#a#1
    good#a#6 nice#a#1 0.822428178005622
    From above, we can see that it works well, similarity.pl is a perl program for computing similarity between two concept, and -type and -file are the parameters.
    Now I want to use java to call these command, and here is my code:
    public static void main(String[] args) {
    try{
    String command="similarity.pl -type WordNet::Similarity::vector -file /home/csunix/fzsu/exam.txt";
    Process p=Runtime.getRuntime().exec(command);
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line=null;
    while ((line=br.readLine())!=null){
    System.out.println(line);
    }catch (IOException ex){
    ex.printStackTrace();
    But noting is print out. I also add the full path for similarity.pl, like /home/csunix/fzsu/similarity.pl in the variable "command", but still the same.
    I also change variable "command" to a string array in this way:
    String command[]={"/home/csunix/nlplib/WordNet/2.1-F7/perl/bin/similarity.pl",
                        "-type","WordNet::Similarity::vector","-file","/home/csunix/fzsu/exam.txt"};
    Again, I get nothing from it.
    Can anyone point out how to solve this problem?
    Thanks,
    Joe

    Crosspost.
    Don't do this if you want help!
    This question is answered on "New to Java".

  • How to call dos commands through HTMLDB buttons

    HI,
    How do i call DOS commands through HTMLDB buttons, or on any process?
    can anyone help me on this?
    THANKS!
    BROKEN

    This article explains how to execute shell commands from whitin pl/sql using java (in the database).
    http://www.oracle-base.com/articles/8i/ShellCommandsFromPLSQL.php
    It is for Oracle8i but should still work. This still looks like a good way to go.
    ~Dietmar.

  • Calling Linux Command From Pl/SQL

    Hi!
    I need to create a trigger which will call a linux command. Basically, when a value changes in a table, I would like to run a linux script. The value in the table can change anytime during any day. I had a look at DBMS_JOB.SUBMIT but I'm not sure if this will work for me because I only need to run the linux script once - when the trigger is fired. I also don't wanna clutter the job queue. Please help. Thanx in advance.

    You didn't mention if you're looking for synchronous or asynchronous execution of your Linux command. Should the program wait for it to complete?
    If it has to be synchronous, you'll need to look into using the external program listener. If asynchronous is ok (and it usually is) and then I'd go with scheduling a job - don't worry about "cluttering up the job queue".
    If you're on 10g or later, don't bother with DBMS_JOB - use DBMS_SCHEDULER instead, which makes it much easier to execute an external process.

  • How to call systems commands

    Hello guys,
    I will like to know if there is a way of calling system command and shell commands in windows without making use of the Runtime Class in java.
    Also when ever i call a system command or shells commands from my java application using the runtime class, it open the a new shell(Command Prompt) before executing the command.
    I await your response. Thanks in advance.
    Joe.

    How about this? http://onesearch.sun.com/ClickThru?qt=without+command+window&url=http%3A%2F%2Fforum.java.sun.com%2Fthread.jsp%3Fforum%3D31%26thread%3D440546&pathInfo=%2Fsearch%2Fdevelopers%2Findex.jsp&hitNum=2&col=devforums

  • How to call OS Commands through PL/SQL ?

    Is there any package to call OS (Solaris) commands from PL/SQL ? I want to do who cammand within PL/SQl Stored Proc and get the output inserted into a table. How can I do this ?

    Why don't you want to use Java? There are certainly alternatives, but without knowing why you don't like the Java solution, it's rather hard to know whether those alternatives make any sense in your environment.
    - You can make an external procedure call to a DLL (or shared library) installed on the server
    - You could use AQ or potentially DBMS_PIPE to communicate with external applications
    - You could use UTL_TCP to interact over the network with a machine
    - You may have other options depending on specifics about the call you're trying to make and the software you have.
    - In 10g, you could use the DBMS_SCHEDULER package to call OS commands
    Justin

  • How to call system command in vc++?

    i wish to call the netsend command in the program but i dont know how to do it? can you please show me the actually of calling net send command in the way of mfc?

    Take a look at the Win32 CreateProcess, ShellExecute, and ShellExecuteEx functions.
    - Elton

  • How to call TimesTen "command" from program

    I m trying to call command "dssize" from my java program.
    I can call any built in procedure with callable statement, but for "dssize" is not working. is that possible?
    Thank you and best regards.

    'dssize' is a ttIsql command and so cannot be called from an application. The information reported by dssize comes from various columns in the SYS.MONITOR table. All you need to do is SELECT those columns and use the values.
    Chris

  • How to call cmd commands in OS COMMAND

    Hi
    I need the syntax for calling a cmd commands using OS Command tool.
    Thanks
    sri

    I was trying to say you incase you are using the xcopy using the server agent when you mention the path please specifiy the complete directory path , since ODI run the scripts from oracledi/bin path .
    Now when you are running cezar command , you dont need to have the file . it copy the directoy liste into that txt file.

  • How to call system command to launch Windows Explorer

    Hello,
    I'd like to open the windows explorer from java.... anyone know how this is done?
    thanks

    thanks for ur reply.
    i get the following exception when i run the code provided...
    java.io.IOException: CreateProcess: iexplore.exe error=2
         at java.lang.Win32Process.create(Native Method)
         at java.lang.Win32Process.<init>(Win32Process.java:66)
         at java.lang.Runtime.execInternal(Native Method)
         at java.lang.Runtime.exec(Runtime.java:566)
         at java.lang.Runtime.exec(Runtime.java:428)
         at java.lang.Runtime.exec(Runtime.java:364)
         at java.lang.Runtime.exec(Runtime.java:326)
         at Run.main(Run.java:5)
    Exception in thread "main" also, i needed a way to open exploerer to a preset directory. do i just append the directory location before the iexplore.exe ? anyone? thanks

  • How to use Linux Command on JSP?

    Hi,
    I am trying to get the hostname and username of the JSP server using the Process object. However it doesn't work. Any advice? Thanks.
    Here's the code:
    try
    Process p = Runtime.getRuntime().exec("whoami");
    BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    l_username = stdInput.readLine();
    out.println(l_username);
    Process q = Runtime.getRuntime().exec("hostname");
    stdInput = new BufferedReader(new
    InputStreamReader(q.getInputStream()));
    l_hostname = stdInput.readLine();
    out.println(l_hostname);
    catch (IOException e) {
    out.println("exception happened ");
    e.printStackTrace();
    }

    Writing raw Java code in a JSP instead of in a Java class and having problems with it doesn't make it a JSP problem. This question rather belongs in the 'New to Java' forum or 'Java Programming' forum, depending on the actual problem you struggle with.
    By the way, why tight coupling your application with the underlying platform by using platform-specific commandline stuff instead of just using the methods the Java API provides, such as those from java.lang.System and java.net.InetAddress and so on?

  • Calling CL command in java

    I would like to know how to call cl command in java to delete a file in my server. thanks

    Use the java.lang.Runtime interface. The exec() method allows you to execute commands from the command shell:
    String cmd = new String[2];
    cmd[0] = "cl";
    cmd[1] = fileToDelete;
    Runtime rt = Runtime.getRuntime();
    rt.exec(cmd);

  • How can i call shell command in JSP?

    My OS is Linux, and my WWW server is Tomcat,
    can i call shell command , such as gcc or setup,
    If yes, how can i? Would you give a source example?
    Thank you!

    Hi,
    I did this simple class to launch a process and after the cose there is an example to use it:
    import java.io.*;
    import java.util.*;
    public class ProcessExec {
    private String[] command;
    private StreamGobbler errorGobbler;
    private StreamGobbler outputGobbler;
    public ProcessExec() {
    public ProcessExec ( String[] command) {
    this.command = command;
    this.errorGobbler = null;
    this.outputGobbler = null;
    public String[] getCommand() {
    return ( this.command );
    public void setCommand( String[] command ) {
    this.command = command;
    public void exec() {
    try{
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(this.command);
    // any error message?
    errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    }catch (Throwable t){
    t.printStackTrace();
    }//catch
    public String getErrorMessage() {
    while ( this.errorGobbler.isAlive() ) {;}
    return ( this.errorGobbler.getResult().toString() );
    public String getOutputMessage() {
    while ( this.outputGobbler.isAlive() ) {;}
    return ( this.outputGobbler.getResult().toString() );
    } // ProcessExec
    Example to use it:
    try {
    ProcessExec pe = new ProcessExec( command );
    pe.exec();
    errorMessage = pe.getErrorMessage();
    outputMessage = pe.getOutputMessage();
    } catch ( Exception e ) {
    System.out.println (e);
    As you can guess the string "command" must be the command you have to launch and errorMessage and outputMessage will contain the error and the output that the process will generate.
    Try it; i hope this will help you.
    Cheers.
    Stefano

Maybe you are looking for