Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Running A Java Program

Mathematical

Silver Coder
Hey there.

If you're just starting to learn Java, then I suggest you read this tutorial. You probably write all of your Java code inside an IDE(Eclipse, IntelliJ, NetBeans, etc.) and also run that code inside the IDE. The thing is, you're gonna want to acquaint yourself with compiling and running Java code through a terminal, as this will come in handy later for things like JAR files(JAR files aren't covered in this tutorial). Well then, buckle your seat-belt and let's head right in.

First of all, write a simple Java program. We'll just create a simple Hello World program for now, seeing as we don't want to take too long to write a test program to teach ourselves on how to compile and run a Java program. Simply write this:
Java:
public class Main {
     public static void main(String args[]) {
          System.out.println("Hello world!");
     }
}
Save that, then open-up your terminal(Command-Prompt for Windows users. Note that for Windows users, you'll need to have Java configured to your PATH environment. There are tutorials online on how to do this). When you've done that, write this into your terminal:
javac Main.java

This will run the Java compiler. Give it a few seconds and when it's done, you'll have this file: Main.class[/CODE] This is a .class file. To put it simply, when your Java program gets compiled, every class inside the program gets compiled into one of these files. Then when it comes to running your program, if [ICODE]Main.class needs to call a method from Something.class, Java will look for the class file that contains that method. Once found, the method can finally be called. If Something.class gets deleted or it simply cannot be found, Java will throw an error when you try to run it.

Note: When you do compile your Java program, you don't need to write the names of every file down. If Main.java needs to call a method from Something.java, you only need to compile the Main class. The Java compiler will then find the rest of files and compile them too.

Anyway, to run your program, simply write: java Main - Do not insert ".class" or ".java" at the end. Ignore the file extensions.

Then hopefully, "Hello world!", will have been printed out into your terminal. Now that's you. You've successfully learned how to compile and run your Java programs in a terminal. This will come in handy for later when you're learning how to create JAR files(Executable Java programs that rely on the JRE to be ran). Just remember that while nearly all of your work in Java will be done in an IDE, it's useful to know how to work with Java in a terminal.

Thank you for reading.
 
Back
Top Bottom