Skip to content

Instantly share code, notes, and snippets.

@Loule95450
Last active December 4, 2022 04:47
Show Gist options
  • Save Loule95450/75ae06ff4cec0a84ca9cfb4a0c73371c to your computer and use it in GitHub Desktop.
Save Loule95450/75ae06ff4cec0a84ca9cfb4a0c73371c to your computer and use it in GitHub Desktop.
How to compile a .java file to .jar

How to compile a .java file to .jar

Create a file "Manifest.txt" in the root of the project with the code below Open the Terminal/CMD and paste the commands located in compiler.sh file

Things to change

Manifest.txt

Change the Main-Class variable with your package name and .java name file Actually : Main-Class: com.company.{.java name file} Actually : Main-Class: me.loule.Main

On Terminal

Change the direction of the directories Actually : javac -cp src/ {locate your java file}/*.java -d bin Exemple : javac -cp src/ src/com/company/*.java -d bin n

Change the direction of the directories Actually : jar -cvfm build/{Name of your Jar file}.jar Manifest.txt -C bin/ . src/ Exemple : jar -cvfm build/MyGame.jar Manifest.txt -C bin/ . src/

Change the direction of the directories Actually : java -jar build/{Name of your Jar file}.jar Exemple : java -jar build/MyGame.jar

It's important to have Java installed on your computer

# Create a class from the .java file
javac -cp src/ {locate your java file}/*.java -d bin
# Create build folder
mkdir build
# Compile using the .class file and manifest.txt to create a .jar file
jar -cvfm build/{Name of your Jar file}.jar Manifest.txt -C bin/ . src/
# Run MyFile.jar
java -jar build/{Name of your Jar file}.jar
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.company.{.java name file}
@usr577
Copy link

usr577 commented Aug 30, 2022

I actually have java installed on my laptop nut it still says: "The command jar is either misspelled or could not be found." And I couldn't ind any information on how to install it on Windows 10/11. How did you install it or do I have to move it to WSL every time I want to compile it, compile it and then move it back? Also I tried finding a msys2/mingw64 or 32 executeable that could handle it but sadly no luck there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment