Java equivalent of /usr/lib and /lib? - java

I am just wondering, if java has something similar to /usr/lib in C/C++, then we won't have to do
java -cp "lib/*" mypackage.MyClass
Instead we can put all our favorite jar files into some folder like /java/lib/ and do
java mypackage.MyClass
Standardization of such a location also saves you the trouble of having to put the same set of frequently used libraries into many projects repeatedly.
Does such a feature exist already?

What you are talking about is called the extension folder. Here are summarized some pros and cons about its usage.

Possible other ways ways:
Use the CLASSPATH environment variable. If -cp or -classpath parameters are not specified, the value of the CLASSPATH environment variable will be used as classpath (if it exists). More on setting the CLASSPATH: PATH and CLASSPATH
The META-INF/MANIFEST.MF file inside jars can specify the class path. Add the following line to specify required libraries:
Class-Path: lib/myjar.jar lib/someotherjar.jar
More on this: Adding Classes to the JAR File's Classpath

Related

How do I run a JAR while including another JAR as class path? [duplicate]

What is the difference between running a Java application withjava -cp CLASSPATH and java -jar JAR_FILE_PATH? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according to their machine resources usage)?
Which one will cause JVM to spawn more threads while trying to run the application?
I prefer the first version to start a java application just because it has less pitfalls ("welcome to classpath hell"). The second one requires an executable jar file and the classpath for that application has to be defined inside the jar's manifest (all other classpath declaration will be silently ignored...). So with the second version you'd have to look into the jar, read the manifest and try to find out if the classpath entries are valid from where the jar is stored... That's avoidable.
I don't expect any performance advantages or disadvantages for either version. It's just telling the jvm which class to use for the main thread and where it can find the libraries.
With the -cp argument you provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled or run. With -jar you specify the executable JAR file that you want to run.
You can't specify them both. If you try to run java -cp folder/myexternallibrary.jar -jar myprogram.jar then it won't really work. The classpath for that JAR should be specified in its Manifest, not as a -cp argument.
You can find more about this here and here.
PS: -cp and -classpath are synonyms.
When using java -cp you are required to provide fully qualified main class name, e.g.
java -cp com.mycompany.MyMain
When using java -jar myjar.jar your jar file must provide the information about main class via manifest.mf contained into the jar file in folder META-INF:
Main-Class: com.mycompany.MyMain
java -cp CLASSPATH is necesssary if you wish to specify all code in the classpath. This is useful for debugging code.
The jarred executable format: java -jar JarFile can be used if you wish to start the app with a single short command. You can specify additional dependent jar files in your MANIFEST using space separated jars in a Class-Path entry, e.g.:
Class-Path: mysql.jar infobus.jar acme/beans.jar
Both are comparable in terms of performance.
Like already said, the -cp is just for telling the jvm in the command line which class to use for the main thread and where it can find the libraries (define classpath). In -jar it expects the class-path and main-class to be defined in the jar file manifest. So other is for defining things in command line while other finding them inside the jar manifest. There is no difference in performance. You can't use them at the same time, -jar will override the -cp.
Though even if you use -cp, it will still check the manifest file. So you can define some of the class-paths in the manifest and some in the command line. This is particularly useful when you have a dependency on some 3rd party jar, which you might not provide with your build or don't want to provide (expecting it to be found already in the system where it's to be installed for example). So you can use it to provide external jars. It's location may vary between systems or it may even have a different version on different system (but having the same interfaces). This way you can build the app with other version and add the actual 3rd party dependency to class-path on the command line when running it on different systems.
There won't be any difference in terms of performance.
Using java - cp we can specify the required classes and jar's in the classpath for running a java class file.
If it is a executable jar file . When java -jar command is used, jvm finds the class that it needs to run from /META-INF/MANIFEST.MF file inside the jar file.

how can i set classpath for external jar files in java?

I am using java1.6 without using any IDE.Now i want to use java Mail API for my purpose.So, i copied Mail.jar into d:\externaljar folder.
And also i have set the classpath as set classpath=%classpath%;d:\externaljar;
my jdk installation folder is : c:\programfiles\jdk1.6.
But i faced package javax.mail does not exist during compilation.
Please Guide me to get out of this issue?
The jar file itself must be in the classpath, and not just the directory containing it.
And the CLASSPATH environment variable is CLASSPATH, not classpath. My advice would be to never use it, though. Always use the -classpath (or -cp) option with javac or java to pass the classpath.
I prefer the -cp option over the global CLASSPATH environment variable:
java -cp .;d:/externaljar/mail.jar my.application.App
I'd recommend against setting CLASSPATH and instead use the -cp flag:
javac -cp .;d:\externaljar\mail.jar whatever/package/YourClass.java
You may also use wildcarding:
javac -cp .;d:\externaljar\* whatever/package/YourClass.java
Running is the same thing, except you provide the classname with the main method.
java -cp .;d:\externaljar\* whatever.package.YourClass

java: importing, class path, and packages

I have a file which imports org.w3c.dom.Document. Compiling and running is fine, but I don't understand how it knows where to find this package and I'm just curious how it works. I used the locate command to try and find org.w3c.dom but I get nothing. Where are these packages located? It seems to me that the right place to look would the CLASSPATH environment variable since my search results seem to be suggesting that. Is this correct? In any case, I don't know how to find out what my CLASSPATH variable is. It doesn't seem to be an environment variable that my shell knows about.
That would be part of the core libraries (rt.jar), so it'd be wherever you installed the java JRE; specifically under $JAVA_HOME/jre/lib
You can look inside the .jar files using the jar command. To see the class you mention, you can do:
jar tvf rt.jar
This lists all the classes in that jar.
Note that this location is automatically searched by the JVM - it's not needed nor included in the CLASS_PATH environment variable. (You could add it, but it would simply be redundant)
Edit for clarity:
The JVM includes <Where_you_installed_jdk>/jre/lib and <Where_you_installed_jdk>/jre/lib/ext by default. Anything else has to be explicitly added by you via either passing it to java directly via the -cp option or adding it to the CLASS_PATH environment variable.
The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
The JVM finds classes using classpath settings where alll paths to required packages are set. The classpath could be set with a number of ways. The first mentioned by you is CLASSPATH environment variable. It is optional and can be unset. The second way is an explicit option "-cp" for "java" executable.
Also some JRE runtime jars are added to classpath by default implicitly so you don't need to search and add standard packages by yourself (particulary the one you mentioned in your question).
try compiling messconvener.java like this from its own directory
javac -d ..\..\. -cp ..\..\. messconvener.java
-d - creates directory structure for your package
-cp - provides class path for user file, where it can find user defined classes

How can I add a .jar file dependencies when building it with the command line tool?

Pretty straightforward question. Can it be done without the use of Ants or Maven? (And by that, I mean the command line tool specifically)
Please notice that I don't want to create an uberjar, I just want the archived unit to "know" where its external dependencies are.
You can make it through META-INF/MANIFEST.MF. You can add other jars to the classpath like this:
Manifest-Version: 1.0
Main-Class: org.domain.MyMainClass
Class-Path: lib/slf4j-log4j12-1.5.8.jar lib/slf4j-api-1.5.8.jar
I believe, that it works only if you define Main-Class and start your application like this:
java -jar my-app.jar
Also notice, that classpath paths are relative to the main jar. So in my example directory structure should look like this:
my-app.jar
lib
slf4j-log4j12-1.5.8.jar
slf4j-api-1.5.8.jar
Presuming you're talking about a command line invocation of javac, what you're talking about is "can I provide libraries as arguments to javac to fulfill requirements during compilation".
Top entry for man javac says
-classpath classpath
Sets the user class path, overriding the user class path in the
CLASSPATH environment variable. If neither CLASSPATH or -class-
path is specified, the user class path consists of the current
directory. See Setting the Class Path for more details.
Effectively I suspect you just need to say
javac -classpath path/to/library1.jar Main.java
I think what you are looking for is a manifest file, Look here for more details
http://download.oracle.com/javase/tutorial/deployment/jar/downman.html

Java - Difficulty installing program from 3 separate .jar files (involves CLASSPATH)

I'm having a little trouble running some Java code, which requires three .jar files to be used. I'm at a lost as to what to do with them--I've tried setting the CLASSPATH (and following the instructions for how to do so in the readme files), but to no avail.
I was wondering if someone could walk me through it? I'd imagine three .jar files would be an easy install for someone who knows what they're doing.
If it helps, I'm using Ubuntu pretty much right out of the box (but I do have JDK and Eclipse installed!)
Runtime library: http://cogcomp.cs.illinois.edu/download/software/20
Additional .jar needed: http://cogcomp.cs.illinois.edu/download/software/23
Program I ultimately need to run: http://cogcomp.cs.illinois.edu/download/software/26
If you're willing to help, I can't thank you enough--you deserve a million kudos!
G
Those are all JAR files. When you execute a JAR file by doubleclicking or using java -jar, the CLASSPATH environment variable and the -cp and -classpath arguments are ignored. The classpath should be defninied in META-INF/MANIFEST.MF file of the JAR. In this particular case, only the second and third JAR have a Class-Path entry in the manifest file:
Class-Path: LBJ2Library.jar
Which is the first JAR. The classpath is telling that it is expecting the LBJ2Library.jar to be in the same folder as the JAR you'd like to execute (either the second or third one).
So, just drop them all in the same folder and execute by java -jar LBJPOS.jar.
If you are using java -jar to run your jar files, then the CLASSPATH variable is ignored. If you are using java -jar, you have two options:
Combine the three jars into one jar.
Run the main class directory and don't use -jar.
Use of the CLASSPATH environment variable is generally discouraged nowadays. This is how it's done (on Linux):
java -cp library1.jar:library2.jar:mainapp.jar <fully qualified name of main class>
You need to set the CLASSPATH .place all the 3 jars in a folder , name it as lib
See below to set classpath
set CLASSPATH=%CLASSPATH%:lib;

Categories