Skip to content
Advertisement

Compiling a java project from linux command line

I am having trouble understanding how to actually use the command line to run a big java project ( by big I mean with multiple files and folder).

Imagine I have a project containing :

JavaScript

All my life people have done the makefile for me. I just code the java src with vim and compile and run with make. Now there is no makefile ! I compile with maven (that I am still understanding how it works.). After compiling with maven (I just run maven compile). I then have a new folder named target.

JavaScript

Now how can I run test1 ? I tried using java -classpath =... test1 but I always get errors …

If can someone help me (or just give me some resources so I can finally understand basic project structuring and scripting) it will be amazing. Thank you !

Advertisement

Answer

Here is a minimal working example to compile java source files from multiple locations and pack them in a single runnable jar.

JavaScript

The contents of java files appear at the end for completeness. Here is the makefile.

JavaScript

See that all java source files are compiled within the same rule, and the corresponding *.class files are put in a dedicated bin directory. To emphasize that everything needed to run the jar is inside it I completely removed the bin directory and the manifest.mf. Now you can run the program with

JavaScript

Here are the java files for completeness:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement