Search code examples
javaexternaljavac

Compile Java Files And External Lib Linux


I've a Java project in IntelliJ which builds and executes successfully. Now I would like to package the code (i.e. package it so it can be executed in a linux environment).

The directory structure is:

../parser/src/com/test1/java
bash-4.2$ ls -R
external  Jparser  Main.java

./external:
java-json.jar

./Jparser:
JsonParser.class  JsonParser.java

So far I've executed javac -cp ../external/java-json.jar Parser.java

What is the next step? Is it class path of Parser.java to main?

Edit: I'm specifically interested to learn how to to the above in command line for better understanding of Java. I love gradle, maven, IDEs, but they all have sorts of black box magic which is often convenient but leaves the beginner user thinking they know Java builds....


Solution

  • It is not clear whether Jparser is a package inside your main module, or it is a sub-module directory.

    1. If this is a package inside your only module, then

    javac -cp ../external/java-json.jar Main.java Jparser/JsonParser.java

    jar cvf my.jar Main.class JsonParser

    1. If this is a module, then I'd copy Main.java and JsonParser.java to another directory and then

    javac -cp ../external/java-json.jar *.java

    jar cvf my.jar *.class

    Then to run it: java -cp my.jar:../external/java-json.jar Main

    A better approach to organize the build would be to use Maven https://maven.apache.org/ or Ant http://ant.apache.org/ IDEA can generate Ant builds using Build->Generate Ant Build.