George
George

Reputation: 30441

Compiling Java into application in VSCode on Mac

I'm new to Java, and new to how Java's compiling stuff works with its Virtual Machine, so please write your answer in simple terms.

I am using VSCode (on macOS) to make a Java application. I am using Eclipse New Java Project to create my projects, which sets up a file structure like so:

TestApp
│
│-- .vscode
│-- bin
╵-- src
  ╵-- app
    ╵-- App.java

Now I am able to compile my simple project, and it prints within vscode:

package app;

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello!");
    }
}

How do I now go about running it similar to an application? I have had a long look around, and found things about creating a .jar file, but I don't get what this is doing.

Is the .jar an executable? When I try click on it I get an error, and after searching that up, I saw that you need to add the main class somehow, and I keep getting lots of errors and nothing is working from the many solutions on Stack Overflow.

In simple terms, how do I compile a Java project so I can open it like an app instead of compiling within vscode every time? Is there an option within vscode which makes this .jar and stuff for me?

Upvotes: 0

Views: 376

Answers (1)

LethalLadders
LethalLadders

Reputation: 156

For running in the terminal (and specifying where the main method is):

jar -cvfe Test.jar App ./app
java -cp Test.jar app/App

For including the entry point (so that you can click on the .jar file to run it):

jar -cvfe Test.jar app.App ./App
jar cfe Test.jar app.App *

Upvotes: 1

Related Questions