Results 1 to 13 of 13
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Read from command line

    I am trying to build a simple program to read input from the command line, so far I have in my main method:

    public static void main(String[] args) {
    String input = null;
    // prompt the user to enter their name
    System.out.print("Enter your name: ");

    // open up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    // read the username from the command-line; need to use try/catch with the
    // readLine() method
    try {
    input = br.readLine();
    } catch (IOException ioe) {
    System.out.println("IO error trying to read your name!");
    System.exit(1);
    }
    System.out.println("Your name is "+input);


    }


    For some reason, its not waiting on my input, its printing out "Enter your name: null"

    I'm not sure what's going on, I thought System.out.print allowed me to print to the screen, and then I can use "input = br.readLine" to set input variable to whatever they enter.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Are you sure?
    Have you saved your code?
    Have you compiled the latest version of your code?

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    positive.

    I compiled it and ran it, and it says "Enter your name: null" and then goes to a new line with a blinking cursor.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Hmmm.

    Clutching at straws: delete the .class file and compile again.

    Provide details of how you are compiling and running the program.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    Im using Netbeans to create a .jar file, and then running it from the command line:

    java -jar "C:\Users\MyCPU\Desktop\ProjectFolder\MyProgram\di st\CommandLineApp.jar"

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Compiling your code and running from the command line (no jar) works fine.

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    hmm..

    I get a NoClassDefFoundError when I try to just put the code that I gave you into a "Main.java" file and then do a javac Main.java (compiles fine), and try to run it " java Main"

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Post your FULL code.

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;

    /**
    *
    * @author Steve
    */
    public class Main {



    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    String input = null;
    // prompt the user to enter their name
    System.out.print("Enter your name: ");

    // open up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    // read the username from the command-line; need to use try/catch with the
    // readLine() method
    try {
    input = br.readLine();
    System.out.println("Your name is "+input);
    } catch (IOException ioe) {
    System.out.println("IO error trying to read your name!");
    System.exit(1);
    }


    }

    public static synchronized void playSound(final String url) {
    new Thread(new Runnable() {
    public void run() {
    try {
    Clip clip = AudioSystem.getClip();
    AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getReso urceAsStream(url));
    clip.open(inputStream);
    clip.start();
    } catch (Exception e) {
    System.err.println(e.getMessage());
    }
    }
    }).start();
    }


    }

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I just stuffed your code (without all the extra crud) into a jar file and it ran fine for me. So I can only assume that once again you are not using the code you think your are in the jar file. Or netbeans is doing something hinky when it creates the jar file.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by sehudson View Post
    hmm..

    I get a NoClassDefFoundError
    Which class? When you get an error copy and paste the full message as it contains more valuable information than just what error it is.

  12. #12
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    weird...im not sure what else I can do.

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    working now..I'm not sure what was happening the other time..Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  2. can i run line by line command in netbean?
    By choconlongxu in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 09:41 PM
  3. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  4. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •