How to use Eclipse generated .classpath file to specify the external libraries? - java

I need to start a Java application using a .bat file . Here is what I have so far, and it works fine
java -cp ".;C:\someLibrary.jar;C:\someLibrary.jar;..." Main
The problem is there are too many external libraries, and Eclipse already generates a .classpath file referring to all those libraries. Is there any way I could use that Eclipse generated .classpath file in my batch, so that I don't have to list all the libraries in the java command above ?
Can I use something like this
java -cp ".;C:\ ..\pathToEclipseFolder.classpath" Main
The reason I am asking this is because I will eventually end up updating some of those external libraries. And I want to still be able to use the original .bat file

If your Java application needs the libraries, then you must specify them in the classpath of your .bat file.
A couple of considerations:
1) Maybe you have more libraries listed in your Eclipse classpath than you actually need
2) Java6 and higher allows you to specify a directory, instead of requiring you to specify each individual library in that directory:
java -classpath ".;c:\mylib\*" MyApp

Related

Create Java App Install Package

First, I did look through the list of suggested questions generated by the keywords in my question but did not find anything relevant or helpful.
New to Java programming (not new to programming) so I don't know what useful tools there might be out there. I have a java console app, written using the intellij IDE. After testing and debugging, I am ready to deploy for a demo. Didn't find anything in the IDE that would let me do this!
I would now like to create a couple of installers - one for windows and one for linux. What do I do? I gather I just need the classfiles, but it would be nice to create an icon which would call the app with the right commandline options for the java.exe. As well, I have dependencies on log4j and jnetpcap (.dll requirements there)... how do I handle getting those support libs deployed - can I use the same installer or do I install them separately?
First things first, you need to JAR those class files. This is the standard way to package files in Java. A typical command would be:
jar cvf MyApp.jar *.class
Next you need to add a manifest to the JAR indicating the entry point into your program. Create a file called manifest.txt and add this line:
Main-Class: MyApp
MyApp would refer to the class name that contains the main() method. Now make the JAR again, this time specifying the manifest:
jar cvfm MyApp.jar manifest.txt *.class
On Windows, you can look into using Launch4J. You can use it to wrap your JAR in a EXE and specify that it runs as a non-GUI, console app.
In Linux, you can include a shell script along with your JAR to execute it. Place the script in your path. For example:
#!/bin/bash
java -jar MyApp.jar
It would be additional work to add dependent libraries to the mix as well as create installers. Seems too broad to include all in one question, but hopefully this will get you started.

How to run a custom jar file copy to classpath command using java?

I have a jar file which I'm using to run as an application. However, every time I run my application I need to add -cp "jar name" to compile it and then run the java application.
How can I make a permanent change so that I don't have to use the command line commands for this task? Can I put that in certain lib location?
Note: I don't have to use any IDE. It has to be from a terminal.
If you are on Linux/ Mac, add the following to your ~/.bash_profile
export CLASSPATH=$CLASSPATH:/path/to/jar
If you are on windows, use this..
set CLASSPATH=%CLASSPATH%;\path\to\jar
You can read more about it here and here
Add your custom JAR in the CLASSPATH. Steps to setup and verify are available here: JAVA PATH & CLASSPATH
you may copy your lib or additional jars to JAVA_HOME/lib/ext
or any directory that is in system variable CLASSPATH
One more solution: extension libraries, put your jar into $JAVA_HOME/lib/ext
One needs to be careful - every java application will pick up this jar

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
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
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
the developing source code to dev branch
the stable source code to release branch
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:
fetch the latest stable source code in the build server
build to executable files in the build server (whatever you do)
send the package to the target server
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.
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.
I would do the following:
create a dedicated directory for this program. Copy the dependent .jar files to that directory
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:
you can set regularly used JVM parameters easily (memory options etc.)
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.
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.

Provide/Export executable file to client

I just completed working on a client's tool, which uses Jfreechart jar, and dll and a lib file for JNI interface. Now I would like to export/ provide a executable file to client, I tried to click on java project folder and export Java>Runnable Jar file (extract required libraries into generated JAR) , a Jar file is exported with some Warnings. However, we are not able to run the file on client's machine. How can I fix this, obviously I don't want to provide complete Java project and ask client to run from and IDE. Please provide me inputs.
Since Java is a cross-platform environment, it doesn't really have "executable files" like EXEs or anything. Instead, you run your .jar file with the Java runtime. For example:
C:\>java.exe MyProgram.jar
Of course some operating systems will just do this for you once the Java runtime is installed.
There are some programs to convert .JAR to .EXE, but I believe they just compile a thin wrapper that calls into the Java runtime.

Create java application jar file in eclipse

I have created a java application in eclipse, wich needs comm.jar and jexcel.jar and .property files so i have added to libray. I want to make a jar file out of my java appliction, including the external jar files added to the appliction.
How can I do it? To run serialport programs I have copied win32.dll into java_home/bin and comm.jar into java_home/jre/lib and javax.comm.properties into java_home/jre/lib, but when delivering the product it should run only by needing the jre.
How can I solve this? Please help me.
Thanks in advance,
suma
Although your question is not totally clear I suggest using the Fat Jar Plugin should allow you to achieve what you want.
You can use File > Export > Executable Jar which includes all libraries. There is also a checkbox to generate an ant build file as well as the jar in order to customize it further (I for instance make all the paths relative and remove the main-class flag).
You have two "path" issues. The Java Classpath and the path from which dlls are loaded.
If you were using a Java EE app server or OSGi then controlling these paths is addressed by the respective runtimes. Both Java EE and OSGi are likely to be overkill for small projects.
In which case you are delivering:
Your application JAR
The dependent jars
The DLLs
I suggest that on installing your app you place these artefacts into a suitable directory structure, for example .../myapp/lib for the jars and .../myapp/bin for the dlls. Don't copy them into the infrastructure directories, for example the JRE lib and bin, or into Windows32 - that just leads to version nightmares and mysteries when someone installs a new jre.
Having got that structure, how to control the paths? For the classpath, look at the Manifest.mf file. tutorial
For the DLL path, I know of no good alternative to setting OS level environment variables ** before ** launching the JVM. Hence you need a little batch/shell script to launch your app, setting the PATH appropriately.
You can also check maven.
You can right click on the project and say "Export". Now select "Java" in tree of choices. Under that select "Jar File". It'll guide you through the process and will allow you to export you project as a jar file.
Hope thats what you are looking for.
The recently released Eclipse 3.5 has a Export as runnable Jar which allows to put all dependent jars in a subfolder to the jar file, and get the Manifest right.
It is an adaption of the FatJar plugin. Works nicely!

Categories