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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

System commands in Java

  • 09-02-2004 3:24pm
    #1
    Closed Accounts Posts: 364 ✭✭


    Hello

    I am familiar with the cstdlib in c++ and how you can make system commands using system("pause"), for instance. I want to know is it possible to do this in java?

    I would like to know:

    1: If possible, how is it done?
    2: Is it possible to assign the result of this command to a string variable ( say for instance if I type in "ver", I want to be able to assign the data to a string varialbe, which I can print in my java code. I am doing this because I want to be able to list system properties in my java program.

    Thanks in advance.

    Matt


Comments

  • Registered Users, Registered Users 2 Posts: 1,186 ✭✭✭davej


    Well for a start there are a number of properties that can be read directly by Java. Eg:

    String os=System.getProperty("os.name");

    will get you, you guessed it..the OS name that you are running on.
    Check the Documentation for the full list of properties you can obtain:

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getProperties()

    To run system commands follow the following syntax:

    Process p = Runtime.getRuntime().exec("mycommand");

    BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));

    // read and parse the output from the system command
    String result=null;
    while ((result = stdInput.readLine()) != null) {
    ..........
    }


    while ((result = stdError.readLine()) != null) {
    System.out.println(result);
    }


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    There is a Thread.sleep() command that will have a similar effect to a pause command, stopping the thread for a certain amount of time. If you want to pause until the user presses a button (Enter for example :p), you do a System.in.read(), which, for console apps, will hold the program until something is pressed. :)


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    I've always wanted to know how to do this without using Threads.. Cheers lads


  • Closed Accounts Posts: 364 ✭✭Matfinn


    Thank you all

    A big help fair play :)

    Matt


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    seamus been doing java for 3 years and I've always wondered how to do that properly, thanks.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,548 ✭✭✭Draupnir


    Just to go one step further, how do you then translate the int returned by the System.in.read() into the ASCII representation of the letter?


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Originally posted by Draupnir
    Just to go one step further, how do you then translate the int returned by the System.in.read() into the ASCII representation of the letter?

    Cast it to char,

    ie
    int i = System.in.read();
    String s = "" + (char)i;
    

    For anything more (i.e. more letters), you're better off with a BufferedReader.

    Actually, maybe one of the gurus can help. I was fiddling and vaguely remember a way of changing a byte[] into a char[] (or maybe a String) and also specifying the Character Set to change it into.
    The above solution will only cast it to a char based on the local settings on the machine, so not necessarily ASCII IIRC.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    That seems a little odd
    int i = System.in.read();
    String s = "" + (char)i;
    
    Why create an instance of a string when you only need to hold a single char.
    char character = (char)System.in.read();
    

    Yes I agree a BufferedReader would be more appropriate when >1 character is entered.

    As for your last question, you can pass a byte[] as a constructor parameter to a String. String also has a method to specify the characterset to use, when returning a byte[].

    HTH ;)

    ;-phobos-)


Advertisement