Execute packaged Java code with commands

Even if I looked it up, it didn't come out easily, so I made a note.

Execution command

$ javac -d /path/to/build/dir/ /path/to/javafiles
$ java -cp /path/to/classfiles/ main.Main

Example

Suppose it is such a hierarchy.

.
└── src
    ├── app
    │   └── App.java
    └── hoge
        ├── Hoge.java
        └── fuga
            └── Fuga.java

compile

Compile everything. Specify the output destination directory of the generated file with -d.

$ javac -d build src/*/*.java src/*/*/*.java

It will be like this.

.
├── build
│   ├── app
│   │   └── App.class
│   └── hoge
│       ├── Hoge.class
│       └── fuga
│           └── Fuga.class
└── src
    ├── app
    │   └── App.java
    └── hoge
        ├── Hoge.java
        └── fuga
            └── Fuga.java

The build directory contains the class files properly packaged.

Run

$ java -cp build app.App

Specify the classpath with -cp. The classpath is the root directory for finding class files. Since it is in build, specify it.

In the app.App part, specify the class that contains the main method, including the package name and delimited by dots.

that's all.

Recommended Posts

Execute packaged Java code with commands
Execute Java code from cpp with cocos2dx
Code Java from Emacs with Eclim
Java build with mac vs code
Prepare Java development environment with VS Code
Link Java and C ++ code with SWIG
[JaCoCo (Java Code Coverage)] Use with NetBeans
Execute Java code stored on the clipboard.
Script Java code
Java code TIPS
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Java character code
About Java commands
Static code analysis with Checkstyle in Java + Gradle
[Java] Explanation of Strategy pattern (with sample code)
Make Java code coverage more comfortable with Jacoco 0.8.0
Using Gradle with VS Code, build Java → run
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
Install java with Homebrew
Build Java development environment with VS Code on Mac
Lombok with VS Code
Change seats with java
[Java 11] I tried to execute Java without compiling with javac
Comfortable download with JAVA
Switch java with direnv
Ping commands in Java
Download Java with Ansible
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Let's scrape with Java! !!
[Environment construction] Build a Java development environment with VS Code!
Build Java with Wercker
Build Java program development environment with Visual Studio Code
Endian conversion with JAVA
Monitor source code changes with guard-shell and make & execute automatically
How to decompile apk file to java source code with MAC
Java web application development environment construction with VS Code (struts2)
Sample code to parse date and time with Java SimpleDateFormat
A memo to start Java programming with VS Code (2020-04 version)
Build WebAPP development environment with Java + Spring with Visual Studio Code
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
Digital signature sample code (JAVA)
Java to play with Function
Docker management with VS Code
Try DB connection with Java
Java parallelization code sample collection
Enable Java EE with NetBeans 9
Try gRPC with Java, Maven