35

I was wondering how to make a Java console program. I use Eclipse as IDE. Looking for something similar to C#'s version of a console program.

Tried Google but only found export to JAR and execute from command line solutions. I would prefer to compile and directly test in a console window.

Thanks in advance

7
  • 6
    public static void main(String[] args) { System.out.println("Hello world!"); } Commented Apr 27, 2012 at 4:29
  • So the way to go is to constantly export my project to a JAR and run in a command line window? Commented Apr 27, 2012 at 4:32
  • 4
    In visual studio I can create a console project. When I want to test the new lines it would automatically open a console window. How to achieve this? Commented Apr 27, 2012 at 4:36
  • Could you maybe point me into the right direction? What I should be reading upon? Commented Apr 27, 2012 at 4:38
  • 1
    I recommend closing your IDE for a few moments and learning a little about how java works. See my answer for a very simple Hello World program. Commented Apr 27, 2012 at 4:40

6 Answers 6

38
/*
 * put this in a file named CommandLineExample.java
 *
 */

class CommandLineExample
{
    public static void main ( String [] arguments )
    {
        System.out.println("Hello, world");
    }
}

type the following from the command line to compile it:

$ javac CommandLineExample.java

then you can run it from the command line like this:

$ java CommandLineExample
Sign up to request clarification or add additional context in comments.

1 Comment

For Windows environment pre-requisite setup (adding Java to the PATH etc) see this article - codejava.net/java-core/…
13

I would recommend using a variant of Eclipse Java IDE when working with jar development.

File > New > Java Project

Right Click "src" > New > Package > Name your package pack1.projectname.

Right Click "pack1.projectname" > New > Class > projectname as class name.

Write all the code your heart desires for this project. Java is a very interesting language. Always use packages with your class files, especially when you are sure that you will be importing your own code in other files. So you can do import pack1.projectname.handler.EventHandler;

File > Export > Java > JAR file, or Runnable JAR file > Next > export destination

This will package your jar file, appropriately with a META-INF manifest, also if you are developing an application or plugin, for anything like Minecraft. You will want a plugin.yml and config.yml file. You right click the project folder not "src" in the project explorer. Do New > File > plugin.yml and config.yml. Just a side note.

The reason I always use pack1 as my toplevel package identifier is of course so I can tell which pack I am working with as I update my project. I always do this to easily compare files I have updated, why I updated them, etc. etc. So initial distribution would be pack1.projectname. Next version pack2.projectname. Also, when not updating code in an entire file, you can just import pack1.projectname.handler.EventHandler; import pack2.projectname.builder.RandomFile; pack3.projectname.init.init; These is a very organized way to develop in Java using Eclipse.

Hope this helped you on your quest to beginning your projects in Java. I will not write Java code on stackoverflow unless the person asking shows a snippet of his own project, and just cannot break through his coders block. Practice makes perfect.

Comments

9

How to make a console application in Eclipse.

  1. File - New - Project
  2. Select "Java Project"
  3. Name it "MyApp"
  4. Right click on MyApp/src
  5. New - Class
    • package name, like "com.sample"
    • Class name "MainClass"
    • check box "public static void main()"
  6. add this line System.out.println("hello"); to main()
  7. Right click "MyApp", Run As - Java Application.

3 Comments

If desired, you can also run it directly from the command-line. Right-click it in the Package Explorer and go properties. Find the java file location. Eclipse auto-compiles it each time you save. Open a command-line. Find the java file and the compiled class eg find /Users/john/eclipse-workspace-test/Test/ -name Test.class. Then run it java -cp /Users/john/eclipse-workspace-test/Test/bin/ Test.
If the console tab is not visible, you may need to go Window > Show View > Console. To rerun it, there should be a green play icon in the toolbar. You can also rerun it from the Run menu.
Your answer came as a result of google search. Should be the most voted!
4

You can run a "console program" inside Eclipse.

Given a simple program, e.g.

public static void main (String[] args) {
    System.out.println("hello world");
}

Open the Eclipse console (Window -> Show View -> Console), and run this program. You should see hello world in the console.

3 Comments

That console returns a null in eclipse for me.
You must not have copied exactly what I wrote in my answer into your program.
Start basic. Get something simple to work, then move onto more complicated things. If you are getting a null, then you have a completely new problem.
2

there is nothing special that makes it console, just use the standard output and standard input, no swing or awt and that's it.

once you have the jar file just issue

java -jar file.jar

or if you don't have jars just

java package.name.ClassName

withouth the trailing.class

Comments

2

What you could do is create a JFrame with a text box at the bottom. This will be where you type. You could then add a jlabel any time you press enter. Also, it will move all other existing jlabel output up by the height of the new jlabel. This gives kind of a terminal type window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.