2

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....

Simply_me
  • 2,840
  • 4
  • 19
  • 27
  • 1
    Did you try https://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly to build a jar (with dependencies, if you have ones)? – Roman Puchkovskiy Jul 31 '17 at 18:55
  • @RomanPuchkovskiy thank you for the reference. I'd like to learn how to build it on linux CLi (as a learning experience), and thus remove the black magic of Jet Brains (although I love it). – Simply_me Jul 31 '17 at 19:00
  • You can also create a runnable .jar with [Maven](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven?rq=1) or Gradle – Ivan Pronin Jul 31 '17 at 19:10
  • Is there a way to do it in/command line? – Simply_me Jul 31 '17 at 19:11
  • Please refer to https://stackoverflow.com/questions/5194926/compiling-java-files-in-all-subfolders – James Jithin Jul 31 '17 at 19:20

1 Answers1

0

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.

Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72