Java console program

103,954

Solution 1

/*
 * 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

Solution 2

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.

Solution 3

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.

Solution 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.

Solution 5

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.

Share:
103,954
John
Author by

John

Updated on December 02, 2020

Comments

  • John
    John over 3 years

    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