How do I make a JAR from a .java file?

前端 未结 7 1145
太阳男子
太阳男子 2020-11-29 17:55

I was writing a simple program using a Java application (not application that has projects, but application within a project; .java) that has a single frame. Both o

7条回答
  •  旧时难觅i
    2020-11-29 18:19

    Ok this is the solution I would have liked to find, instead here I write it:

    First create the directory structure corresponding to the package defined for the .java file, if it is my.super.application create the directory "my" and inside it "super" and inside it the .java file "App.java"

    then from command line:

       javac -cp /path/to/lib1.jar:/path/to/lib2.jar path/to/my/super/App.java
    

    Notice the above will include multiple libraries, if under windows use "," to separate multiple files otherwise under GNU/Linux use ":" To create a jar file

       jar -cvfe App.jar App my/app/
    

    the above will create the application with its corresponding Manifest indicating the App as the main class.

    Including the required libraries inside the jar file is not possible using java or jar command line parameters.

    You can instead:

    1. manually extract libraries to the root folder of the jar file
    2. use an IDE such as Netbeans and insert a rule inside post-jar section of nbproject/build-impl.xml to extract the libraries inside the jar. See below.
    
            
            
        
                  
                
          
      
        
    

    the file.reference names are found inside project.properties file after you added the libraries to the Netbeans IDE.

提交回复
热议问题