Tony
Tony

Reputation: 3409

run java console program in unix

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

Answers (5)

qrtt1
qrtt1

Reputation: 7957

Designing for easy deployment is important in my opinion. In our case, there are some components:

  1. store project in the source code management system (git). we break down source code as

    1. the developing source code to dev branch
    2. the stable source code to release branch
  2. use build tool, such as ant or maven, and provide a deploy script in the project. (we will talk deploy script in 3.).

  3. provide deploy script to:

    1. fetch the latest stable source code in the build server
    2. build to executable files in the build server (whatever you do)
    3. send the package to the target server
    4. launcher (close the old app and run the new app) in the target server (via remote ssh command)

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

Watt
Watt

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

Brian Agnew
Brian Agnew

Reputation: 272407

I would do the following:

  1. create a dedicated directory for this program. Copy the dependent .jar files to that directory
  2. write a (short) script that sets the classpath to point to these jars and then executes the main class

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:

  1. you can set regularly used JVM parameters easily (memory options etc.)
  2. it's a pain (and hardly intuitive) to type 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

dopplesoldner
dopplesoldner

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

AlexR
AlexR

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

Related Questions