Reputation: 3409
I have some code in Java using Eclipse and I would like to deploy it to unix envirnment. The program is simple console program that just takes some arguments at the run time, read a file and print out some results.
My question that what is best approach to deploy and run it in unix envirnment. I was just thinking to copy all the classes file to the unix envirnment and create a batch file to run the main class file. Does this sound okay? Or, should I create a runnable jar file?
Also, where should i put the jar files that the prgram is referencing (in classpath)?
Thanks
Upvotes: 1
Views: 3080
Reputation: 7957
Designing for easy deployment is important in my opinion. In our case, there are some components:
store project in the source code management system (git). we break down source code as
use build tool, such as ant or maven, and provide a deploy script in the project. (we will talk deploy script in 3.).
provide deploy script to:
Currently, you think how to package the java, but it is a simple thing just about building and runing. When you talk about deployment, make it as easy as possible. Each time we deploy just to invoke the release script.
PS. I don't like the executable jar. Using un-packaging jars and compiled class can be sending by rsync very efficiently.
Upvotes: 1
Reputation: 3164
I find this quickest of all:
First, create a jar, copy to unix server and change file permission just as dopplesoldner mentioned below.
You can put your library classes and or jar dependencies in a lib
folder
Then execute the jar
java -Djava.ext.dirs=lib/ -classpath yourJar.jar com.yourPackage.yourClass
yourClass
will be the class having main(String args[])
method you wanted to execute.
Upvotes: 0
Reputation: 272407
I would do the following:
Given the above, it's largely a matter of style as to whether you create a runnable .jar or not. It'll be hidden from the user.
I'm suggesting a script because:
java -jar {pathtojar}
etc.By copying the jars to a dedicated directory, you can then use different versions of jars for different scripts (e.g. you may have 2 programs that use two different versions of commons-lang)
You should also (probably) use this script to explicitly determine which version of Java you use to run the program with. As you install/upgrade you don't want to break your programs and the scripts can be configured to explicitly tie down this info.
Upvotes: 0
Reputation: 9499
I think an executable jar file will solve your purpose here.
You should be able to execute it as
java -jar <jarfilename> <arguement1> <arguement2> .... <arguementN>
You can execute the jar file from the current directory itself, just make sure your jar file has executable permissions.
chmod +x <jarfilename>
ls -la
Upvotes: 1
Reputation: 115388
It sounds Ok and will work for you. Just one fix: you are going to write shell script for unix, not batch file.
But you can do better. Typically java classes are packaged into jar file. Jar file is just a zip file with optional META-INF
, directory, MANIFEST.MF
and other stuff. So it is better to package your application into jar and then run it as: java -cp yourjar.jar YourMainClass
.
To create jar file you can use any tool that can create zip or utility jar
that is a part of your JDK. You can also create automatic build using ant, maven, gradle, bildr etc that will help you to package your application.
Upvotes: 0