Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java Runtime.exec() query

Options
  • 20-02-2003 8:24pm
    #1
    Registered Users Posts: 53 ✭✭


    I was wondering if anyone would be able to help me, its not too complicated, however im not good programmer, a newcomer to linux and i couldnt find a decent site that could answer my problem

    I need to be able to read the contents of a directory, and preferably put them in a array of strings, however it doesnt bother me if the data is placed in file, although id like to keep io operations to a minimum.

    basically what i have is

    String[] dir = {"bin/sh", "-c", "ls -l >test.txt"};
    Runtime.exec(dir);

    it gives me an error under the . between Runtime and exec saying that it cannot be referrenced from a Static Context
    I have impoted io also so thats not the problem

    Ant help would be greatly appreciated


Comments

  • Registered Users Posts: 53 ✭✭schlaps


    thats any not ant


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Should you not be iterating through the array executing each command on every pass?


  • Closed Accounts Posts: 110 ✭✭Korg


    java.io.File.list() is what you're after

    http://java.sun.com/j2se/1.4/docs/api/java/io/File.html#list()


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    If I was doing this I'd put the strings into the array and do the following

    String[] dir = {"bin/sh", "-c", "ls -l >test.txt"}; 
    
    for (int i = 0; i < dir.length; i++) 
    {
         Runtime.exec(dir(i));
    }
    
    

    <snip .../> What Korg said!


  • Registered Users Posts: 1,931 ✭✭✭Zab


    You should get the list using the method in File, like Korg said.

    If you're interested in what the error was though: exec() is an instance method, which means that to run it you there must be an instance associated with the call. When you call a method using Classname.method(), method() has to be a static method. ie. it does not need an instance of the class to be run. If this doesn't make sense to you, you should pick up a book on Java. "Thinking in Java" is pretty good, and is available to download for free.

    In the case of Runtime, you get an instance by using the static getRuntime() method.

    Zab.

    PS: Evil Phil - that code would do something totally different... each element of the dir array would get executed in a separate process, which wouldn't have the desired effect.


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Originally posted by Zab
    PS: Evil Phil - that code would do something totally different... each element of the dir array would get executed in a separate process, which wouldn't have the desired effect.

    Okay, I didn't know that :confused:: Why so?


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Originally posted by Evil Phil
    Okay, I didn't know that :confused:: Why so?

    Every exec() call spawns a new process to execute the command passed into it. schlaps was trying to run the whole array as a single command ( ie. "bin/sh -c ls -l >test.txt" ), whereas your code would run three separate processes ( the second one being "-c" ).

    Zab.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    OK, thanks.

    Just as a thought, if it was saved in a script could you execute that?

    Although Korgs way is the way, (way!)


  • Registered Users Posts: 53 ✭✭schlaps


    Cheers lads thats a great help. I ended up using File.list() as Korg said. Although i had another motive for using runtime. I am doing a project which uses a type of Genetic Programming to create corewars warriors, which are an assembly type language.

    However i have to run corewars using Runtime.

    The command in String sCmdls works on the command line but will not execute (i think, not sure, no errors and no output) for some reason. I know runtime works (thanks to zab) because a test directory is created when this code is executed. Im getting no errors, but the results should be piped to score.txt

    Can anyone explain what is happening, and why the corewars-cmd doesnt appear to be working, yet it works on the command line

    HERE IS THE CODE::=

    String sCmdls = "/usr/bin/corewars-cmd -lREDCODE incendiarybomb.red output.red gate.red baselineplus.red > score.txt";

    try {
    System.out.print(sCmdls + "\n");
    Runtime rt1 = Runtime.getRuntime();
    Process proc1 = rt1.exec(sCmdls);
    proc1 = rt1.exec("mkdir test");
    }

    catch (Exception e) {
    System.out.println("Thread1 Error");
    }


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Here's a class I wrote to run a command

    You can use that is you want - this class automatically captures the command output (both from stdin and stderr)

    You can run it directly from the command line as a standa alone program and pass a command to it as an argument - hope it helps:

    import java.io.*;
    import java.util.*;

    public class RunCommand
    {
    private String command;
    private BufferedReader input;
    private BufferedReader err;
    private boolean success;
    private Vector results;
    private static RunCommand myCommand;
    private Enumeration enum;

    public RunCommand(String command)
    {
    this.command = command;
    results = new Vector(0);
    }

    public void run()
    {
    try
    {
    Process p = Runtime.getRuntime().exec(command);
    streamReader inpReader = new streamReader(p.getInputStream(), results);
    streamReader errReader = new streamReader(p.getErrorStream());
    inpReader.start();
    errReader.start();
    p.waitFor();
    inpReader.join();
    errReader.join();
    success = true;
    }
    catch (IOException e)
    {
    System.out.println(e.toString());
    e.printStackTrace();
    success = false;
    }
    catch (InterruptedException e)
    {
    System.out.println(e.toString());
    e.printStackTrace();
    success = false;
    }
    }

    public int getNumLines()
    {
    return results.size();
    }

    public void prepareForReading()
    {
    enum = results.elements();
    }

    public boolean moreOutput()
    {
    return enum.hasMoreElements();
    }

    public String getOutput()
    {
    return (String)enum.nextElement();
    }

    public boolean success()
    {
    return this.success;
    }

    public static void main(String args[])
    {
    myCommand = new RunCommand(args[0]);
    myCommand.run();
    if (myCommand.success())
    {
    System.out.println("There were " + myCommand.getNumLines() + " lines of output");
    System.out.println("The output from the command was:");
    System.out.println("================================");
    myCommand.prepareForReading();
    while (myCommand.moreOutput())
    System.out.println(myCommand.getOutput());
    }
    else
    {
    System.out.println("There was an error.");
    }
    }
    }



    class streamReader extends Thread
    {
    private BufferedReader br = null;
    private Vector v = null;

    public streamReader(InputStream is)
    {
    br = new BufferedReader(new InputStreamReader(is));
    }

    public streamReader(InputStream is, Vector v)
    {
    br = new BufferedReader(new InputStreamReader(is));
    this.v = v;
    }

    public void run()
    {
    String line;
    try
    {
    while ((line = br.readLine()) != null)
    if (v != null)
    v.add(line);
    }
    catch (IOException e)
    {
    System.out.println(e.toString());
    e.printStackTrace();
    }
    finally
    {
    try
    {
    br.close();
    }
    catch (IOException e)
    {
    System.out.println(e.toString());
    e.printStackTrace();
    }
    }
    }
    }


  • Advertisement
Advertisement