Java JAR basics

java beginner. Until you create a jar file with java and execute it.


Overview

① Create an appropriate directory ② Create an appropriate java file ③ Create JAR file ④ Play with the manifest file ⑤ Execution


Details

① Create an appropriate directory

~/sample/

② Create an appropriate java file

Working directory: ~ / sample / Sample.java

package sample;
public class Sample{
  public static void main(String[]args){
      System.out.println("I was able to execute");
  }
}

Try compiling and running it once. Working directory: ~

> javac sample/Sample.java
> java sample.Sample
I was able to execute

③ Create JAR file

Working directory: ~

>jar -cvf Sample.jar sample/
Manifest added
sample/Is being added(Enter=0)(Out=0)(0%Stored)
sample/Sample.class is being added(Enter=428)(Out=305)(28%Shrinked)
sample/Sample.Java is being added(Enter=137)(Out=120)(12%Shrinked)

** Check the contents of the JAR file **

> jar -tvf Sample.jar
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
     0 Sat Mar 14 12:19:02 JST 2020 META-INF/
    66 Sat Mar 14 12:19:02 JST 2020 META-INF/MANIFEST.MF
     0 Sat Mar 14 12:15:34 JST 2020 sample/

   428 Sat Mar 14 12:15:34 JST 2020 sample/Sample.class
   137 Sat Mar 14 12:09:48 JST 2020 sample/Sample.java

④ Play with the manifest file

Working directory: ~ Contents of META-INF / MANIFEST.MF

META-INF/MANIFEST.MF


Manifest-Version: 1.0
Created-By: 13.0.2 (Oracle Corporation)

** Creating a manifest file **

Sample.mani


Main-Class: sample.Sample

** Addition of main manifest attribute **

>jar -uvfm Sample.jar Sample.mani
Manifest updated

Contents of META-INF / MANIFEST.MF after execution

META-INF/MANIFEST.MF


Manifest-Version: 1.0
Created-By: 13.0.2 (Oracle Corporation)
Main-Class: sample.Sample

I wrote Main-Class directly to MANIFEST.MF, but I couldn't execute it.

⑤ Execution

Working directory: ~

> java -jar Sample.jar
I was able to execute

** Other execution methods **

> java -cp Sample.jar sample.Sample

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
I was able to execute

Recommended Posts

Java JAR basics
Java basics
Java basics
Java basics
java programming basics
Object-oriented (Java) basics
Java concurrency basics
java jar classpath command
Java programming basics practice-array
Java Network Basics (Communication)
Muscle Java Basics Day 1
Basics of character operation (java)
Java programming basics practice-for statement
Java
Summary of Java language basics
Java programming basics practice-switch statement
Getting Started with Java Basics
Java Development Basics ~ Exercise (Array) ~
Java
[Java11] Stream Usage Summary -Basics-
[Java basics] What is Class?
Try using Redis with Java (jar)
javac, jar, java command cheat sheet
Java Performance Chapter 5 Garbage Collection Basics
Java learning (0)
[Java] array
Java protected
Rails basics
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Ruby basics
Java array
[Java] ArrayDeque
Ruby basics
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
Fragment basics
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
JPA Basics 1
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
Docker basics