Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Prime624

Thread: Command Line Program

  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Command Line Program

    I have a program that runs in the terminal (I use Ubuntu). It asks the user for a String, does something with it, and terminates.

    It works in the NetBeans terminal, but not outside of it. Specifically, regardless of what I input, it doesn't continue with the program. I enter something, press enter and it just goes to the next line in the terminal.

    Java Code:
    Scanner scan=new Scanner(System.in);
    System.out.println("Enter the music directory:");
    String path=scan.nextLine();
    EDIT: Also, how do I pass in arguments when I run the program (still in Linux)? Is there a way to check if the program was launched from the terminal or not?
    Last edited by Prime624; 02-08-2014 at 01:35 AM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Command Line Program

    I don't know exaclty what is wrong, but just for the sake of ruling out a fluke in java/linux, try using a BufferedReader. Like so:

    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //to read the stream, use:
    br.readLine();
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Command Line Program

    Perhaps it isn't recognizing the line terminator.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Command Line Program

    Quote Originally Posted by zFollette View Post
    I don't know exaclty what is wrong, but just for the sake of ruling out a fluke in java/linux, try using a BufferedReader. Like so:

    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //to read the stream, use:
    br.readLine();
    Tried that and the same thing happened. Enter key did nothing but go to next line.

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Command Line Program

    Quote Originally Posted by jim829 View Post
    Perhaps it isn't recognizing the line terminator.

    Regards,
    Jim
    Does that mean it's not recognizing the enter key as the enter function? How do I fix it?

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,989
    Rep Power
    5

    Default Re: Command Line Program

    The answer to that is probably found within the many complexities of the Linux shell you're using and not so much in Java.

    I can't really believe it though because when I Google the problem, I don't really get any existing occurences of BufferedReader newline interpretation failing on Linux.
    Last edited by gimbal2; 02-08-2014 at 12:24 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Command Line Program

    I am reading that linux uses '\n' (new line) and windows uses '\n' and '\r' (new line and return). This may be the problem. Though, the API says BufferedReader.readLine() supports both.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,989
    Rep Power
    5

    Default Re: Command Line Program

    You are missing MacOS, which uses only \r. But no, that is not the problem. The Linux implementation of the Java Virtual Machine defines the newline to be \n to match its native environment. You can easily check that out by looking at the value in File.SEPARATOR, which is operating system specific. But that discussion is completely besides the point, BufferedReader is designed to handle any type of newline regardless of the OS, since you can still easily get for example a file with newlines that do not match the OS rules. I can't speak for the Scanner class though, I assume it uses the same newline handling logic.

    I can only assume one thing here: Java does not receive the input on the shell at all; for whatever reason.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Command Line Program

    Quote Originally Posted by gimbal2 View Post
    You are missing MacOS, which uses only \r. But no, that is not the problem. The Linux implementation of the Java Virtual Machine defines the newline to be \n to match its native environment. You can easily check that out by looking at the value in File.SEPARATOR, which is operating system specific. But that discussion is completely besides the point, BufferedReader is designed to handle any type of newline regardless of the OS, since you can still easily get for example a file with newlines that do not match the OS rules. I can't speak for the Scanner class though, I assume it uses the same newline handling logic.

    I can only assume one thing here: Java does not receive the input on the shell at all; for whatever reason.
    Ahh, yes, OSX. I am going to boot up my VM and test some things. (Ubuntu 14.0.4LTS) so I should get similar results.

    EDIT: I did a small test, and this seems to work fine in ubuntu:

    Java Code:
    while (true) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Input something: ");
    String s = scanner.nextLine();
    System.out.println("You inputted: " + s);
    }
    It runs in netbeans and in the terminal with this shell script:
    Java Code:
    java -jar Test.jar
    Last edited by zFollette; 02-08-2014 at 09:15 PM.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  10. #10
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Command Line Program

    Found the problem. I was using JarSplice to create a fat jar with my program and my library in it. For some reason, the fat jar doesn't work but the original jar (with library dependent lines commented out) does work. I can't believe I forgot about that. It's a known bug.
    Last edited by Prime624; 02-08-2014 at 10:02 PM.
    gimbal2 likes this.

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,989
    Rep Power
    5

    Default Re: Command Line Program

    Thanks for posting back, it's useful information to keep in the back of the head for the next person that has a similar issue.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 11
    Last Post: 02-18-2012, 04:10 PM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. How do I run my program through Command Line?
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-03-2010, 01:13 AM
  4. Replies: 14
    Last Post: 01-21-2010, 09:10 AM
  5. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 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
  •