Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default how to create console program?

    i have a code like this:
    Java Code:
    import java.io.Console;
    import java.io.IOException;
    
    public class console_test {
    	public static void main (String [] args) throws IOException {
    		Console new_console = System.console();
    	
    		if (new_console == null) {
    			System.err.print("No console detected\n");
    			System.exit(1);
    		}
    	
    		String user_input = new_console.readLine ("enter: ");
    		
    		if (user_input != "0") {
    			System.out.println("welcome, " + user_input + "\n");
    		}
    		
    		else
    			System.out.println("exit now...");
    	}
    }
    and the result is: "No console detected". how to create console program in java? i hope you understand that i'm a newbie... haha...

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: how to create console program?

    That means that the console is not available. Where did you run your program? Have you tried to run it in terminal or command prompt?

  3. #3
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    no i haven't... i thought it would automatically create a console program and call it...

  4. #4
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    btw, how to make the .java file to compiled become an .exe? in which directory it can be found? all i can see in my Eclipse's "workspace" directory is: ".setting", "bin", and "src"

  5. #5
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    hello?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to create console program?

    Correct -- No console is available, and you can't use it there. As to making an ".exe", why do you feel you want or need an exe file? Why not simply create a .jar file?

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    what .jar files does? how to execute it in command prompt?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: how to create console program?

    See the API doc for java command. It has an option: -jar for executing properly build jar files.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: how to create console program?

    First you use the javac command to compile your java source program into class. If you have a HelloWorld.java when you compile it you will have a HelloWorld.class file. Then you can run the .class using java command.

    Java Code:
    $ javac HelloWorld.java
    
    $ java HelloWorld
    There must be a way to configure Eclipse to make the console available in it. Unfortunately I don't understand Eclipse that well.

  10. #10
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: how to create console program?

    Use InputStreamReader with BufferedReader:

    Java Code:
            InputStreamReader convert = new InputStreamReader(System.in);
            BufferedReader stdin = new BufferedReader(convert);
    Then, use the BufferedReader's readLine() to read a string.

    Use Scanner on the string returned from readline to extract the info you want.

  11. #11
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    what do you mean? i'm not talking about basic input / output in console... instead, how to create a console program in eclipse? :D

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: how to create console program?

    As stated by wsaryada, you can run your program on the command line (windows) or the linux terminal. If you want to be able to debug your program in eclipse (using the console tab window) you will need to use the code I gave you.

  13. #13
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: how to create console program?


  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to create console program?

    The java program (virtual machine launcher) also starts a terminal/console for your program while javaw doesn't do that. If a console is created you can use the Console class and the System.in/out/err classes. No class in the SE distribution creates a terminal for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    (using the console tab window)
    you mean "console window"?

    btw, i can't use javac in my command prompt. it said that it was not recognized

    just for information:
    my eclipse version is 1.2.2.20100216-1730

  16. #16
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: how to create console program?

    I'm not on a computer that has eclipse at this time. I meant the console tab window in Eclipse. There's a window in Eclipse that acts like a command window. It located on a tab and its called "console".

    You need to update the path to the location of the jdk, i.e. C:\Program Files\Java\jdk1.7.0_04\bin.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to create console program?

    Quote Originally Posted by chipp View Post
    you mean "console window"?

    btw, i can't use javac in my command prompt. it said that it was not recognized
    If you look up this error message, you'll likely find folks with the same problem, and will also see that it was easily solved by modifying your operating system's path.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to create console program?

    Quote Originally Posted by Fubarable View Post
    If you look up this error message, you'll likely find folks with the same problem, and will also see that it was easily solved by modifying your operating system's path.
    ... or your JDK isn't even installed because Eclipse comes with its own.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: how to create console program?

    i'm trying to search the JDK now... in the meantime, does anyone has a suggested link?

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to create console program?

    The JDK (if it is installed) is installed next to the JRE. Check your path variable: if should point to your JDK/bin directory.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2011, 03:52 PM
  2. jar file running console program
    By newToJava3 in forum New To Java
    Replies: 3
    Last Post: 12-07-2010, 01:28 AM
  3. Replies: 4
    Last Post: 04-14-2010, 05:01 AM
  4. Factorial console program
    By er1c550n20 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 03:07 PM
  5. Ready To Program, Images in HSA Console?
    By habbah in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 04:50 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
  •