问题
how can I make jar for my java files in linux?
I have this situation:
I have in directory src this stuff:
Client.java //source code
GUI.java // source code
miglayout-lib.jar //external lib
icons // folder in which are 20 jpeg pictures
when I want to compile it I use
javac -cp "miglayout-lib.jar:." *.java
when I want to run it I use
java -cp "miglayout.jar:." Klient
//cause Klient is class with main.
How can I make some build file or script (something like make) which will make me one jar file with my application and I'will be able to run it?
Thx.
回答1:
You can use command line utility named jar e.g.
jar cvf classes.jar .*.class
Use online help of jar for more info.
Since jar is a part of JDK it is cross platform, so it works on all operating systems.
But it is very low level. You can use it as an exercise but in real life I'd recommend you to use one of popular build tools like ant, maven, buildr, gradle etc.
回答2:
There are 2 popular options for Java:
- Ant: http://ant.apache.org/ -> With Ant, you have to write your own build script that will call the compile task and the jar task. See Writing a simple buildfile to get started
- Maven: http://maven.apache.org/download.html -> With Maven, you are more declaring some attributes of your build and everything is handled by Maven itself. See Getting Started for more info there. If you want to just double-click on the jar to start it, you need to make the jar executable. See Make The Jar Executable on how to do that with Maven.
来源:https://stackoverflow.com/questions/9597437/how-can-i-make-jar-in-linux