how can i set classpath for external jar files in java? - 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

Related

Java equivalent of /usr/lib and /lib?

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

How to compile servlets from command prompt?

I'd like to compile a very basic servlet from command prompt, but it is always unsuccessful and the compiler tells me the following:
error: package javax.servlet does not exist.
I googled for the solution and I found that I need to include the servlet.jar libraries into my PATH.
I believe I did.
I strongly believe that the location for those libraries in my computer is:
C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
and the end (the relevant part) of my PATH is the following:
%JAVA_HOME%\bin;C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
For me, it looks ok, but it is obviously not. Can anyone tell me what could be the problem?
classpath not path ... and you don't need it as an evironment variable.
You can set the classpath for javac with option -cp or -classpath (several other ways are also available).
javac uses the environment variable CLASSPATH to look for classes, that can be useful and can also be a source for hard-to-track-down-problems.
An example to compile a java file that uses a library(that is classes from outside the standard JavaSE) would be:
javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
if your environmental variable CLASSPATH contains libraries you need you might want to do:
javac -classpath %CLASSPATH%;C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
(please be aware that I don't have access to a windows machine, and therefore haven't tested the idiosyncratic parts of the syntax above)
(also note that in this example "C:\apache-tomcat-7.0.23\lib\servlet-api.jar" is a jar file and not a directory which it might be from your question on your machine)
For command line compiling on windows OS it is a good idea to have the environmental variable JAVA_HOME set correctly and the bin directory of the JDK in the PATH.
I do however suggest getting to write-compile-execute-deploy via/in an IDE for servlet development before figuring out how to do it with just the JDK from a command line.
Java Servlets are not stand-alone executable classes but needs to be deployed into for example tomcat to be tested/used.
First copy the servlet-api.jar file from following path
C:\apache-tomcat-7.0.23\lib\servlet-api.jar;
and paste it in the bin folder of java software by following the path
C:\java\jdk1.6\bin;
Hope now you can successfully compile your servlet program.
1.You can copy your javax.servlet.jar in the jdk1.6\lib folder.
2.You can go to Control Panel\System\Advanced System Properties\Environment Variables
Enter the location of the jar in the CLASSPATH variable as follows:
Then compile and run the servlet.

How to include jar files with java file and compile in command prompt

I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt?
You can include your jar files in the "javac" command using the "-cp" option.
javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java
Instead of "-cp" you could also use "-classpath"
javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java
You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.
Different machines have different methods to set the classpath as an environment variable.
The commands for Windows, Linux, etc are different.
You can find more details in this blog.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Please try on Linux
javac -cp jarfile source file
EXAMPLE :-
javac -cp .:/jars/* com/template/*.java
Syntax will work on windows dos command:
javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
The followings are steps,
Copy all jars and your .java file in a same folder (It will be easy to mention file names instead of mentioning long path. Though you can keep jar and .java in separate folders).
To compile,
javac -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>.java
To execute,
java -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>
I hope this helps!
Try to add all dependency jar files to your class path through environment variable settings or use the below steps:
Open command prompt.
Change directory to the location of you java
file that you would like compile.
Set the classpath for your dependency jar files as shown below:
set classpath=C:\Users\sarath_sivan\Desktop\jars\servlet-api.jar; C:\Users\sarath_sivan\Desktop\jars\spring-jdbc-3.0.2.RELEASE; C:\Users\sarath_sivan\Desktop\jars\spring-aop-3.0.2.RELEASE;
Now, you may compile your java file. (command: javac YourJavaFile.java)
Hope this will resolve your dependency issue.
This will create .class file:
javac -classpath "[jarname with specified path]" [java filename]
This will execute class file:
java -cp [jarname with specified path]: [java filename]
Try This.
javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java
javac -cp jars/jar1:jars/jar2:jars/jar3 abc.java
With -cp command we specify the path where to find the additional libraries which are required to compile the class. jar1, jar2 and jar3, available in jars folder are used to compile abc.java class.
You need to specify the dependencies in compile time as well as runtime
To compile use this format
javac -cp "*.jar;classfile_path" filename.java
Example:
javac -cp "ojdbc6.jar;c:\programs" Main.java
some times making following change works:
java -cp ".;%CLASSPATH%" classfilename
Note: ON Windows. For linux use $CLASSPATH instead.
If you are using Ubuntu:
/opt/JavaServices/sqlite $ export CLASSPATH=/opt/JarFiles/XXXX.jar:/opt/JarFiles/XXXX.jar:/opt/JavaServices/;javac SQLiteSample.java
Go to folder location (Out of package structure)
/opt/JavaServices $ export CLASSPATH=/opt/JarFiles/XXXXX.jar:/opt/JarFiles/XXXXX.jar:/opt/JavaServices/;java sqlite.SQLiteSample
Note: Please see the file locations and package names
Plenty of these answers helped me, but none that were exactly what I needed.
Assumptions:
Windows OS
JAR file and java file are in same directory
javac -cp <jar filename>.jar <filename>.java
java -cp <jar filename>.jar; <filename>
Keep in mind the syntax needs to exactly match. Cannot exclude file extensions or the semi colon.

CLASSPATH set but java compiler cannot find class files

I am running Ubuntu 11.10 and have installed jdk-6u30-linux-i586.bin
and have a directory /usr/local/jdk1.6.0_30 and everything was working
and compiling fine even without a CLASSPATH so long as I had
export PATH=/usr/local/jdk1.6.0_30/bin:$PATH in my ~/.bashrc
and executed java from a fresh new shell (not sure why no
CLASSPATH is needed in my env).
Now I am trying to use the following class libraries:
http://code.google.com/p/google-api-java-client/downloads/list
google-api-java-client-1.6.0-beta.zip
I downloaded and extracted the zip file to a /usr/local/google directory
which now contains all the jar files. I then try to compile the BigQuerySample
from http://code.google.com/p/google-api-java-client/wiki/ClientLogin
$ javac -cp /usr/local/google BigQuerySample.java
and I get:
BigQuerySample.java:1: package com.google.api.client.googleapis does not exist
import com.google.api.client.googleapis.*;
and so on for all the imported packages except for java.io.*;
I know this should be a simple classpath problem but adjusting the classpath
on the command line or in the environment with export CLASSPATH=$CLASSPATH:/usr/local/google
does not get rid of the errors. I have tried jar -tvf *jar for each jar file and the
stuff is there, so why is the java compiler not finding the includes?
Thanks,
John Goche
You need to add the jar to your classpath like this:
javac -cp "$CLASSPATH:/usr/local/google/google-api-client-1.6.0-beta.jar" BigQuerySample.java
Or use a wildcard to add all jars:
javac -cp "$CLASSPATH:/usr/local/google/*:/usr/local/google/dependencies/*" BigQuerySample.java
You may try this:
javac -Djava.ext.dirs=/usr/local/google BigQuerySample.java
You will have to explicitly specify all the references JARs.
javac -cp /usr/local/google/file1.jar:/usr/local/google/file2.jar:. BigQuerySample.java
Same thing while running...
java -cp /usr/local/google/file1.jar:/usr/local/google/file2.jar:. BigQuerySample
When including jars in the classpath either specifically indicate the jars to include or use wildcards to include all jars in a directory. So for your example you could use:
javac -cp /usr/local/google/google-api.jar BigQuerySample.java
or
javac -cp /usr/local/google/* BigQuerySample.java
For more help using including jars in the classpath see this post.

Reference all jars from a folder

I'm executing a java application in DOS command window using something like
java -cp abcclient.jar;junit-4.4.jar;myapp.jar MyMainClass
I need to reference many other jars that are found in a specific folder outside my application folder. Is there anyway I could state a folder name in the above command line to let java refer to the necessary jars from that folder.
Thanks
With java6, you can use a wildcard in classpath entries, so:
java -cp "abcclient.jar;junit-4.4.jar;myapp.jar;..\lib\*" MyMainClass
should work
(There's some problems explained here though http://javahowto.blogspot.com/2006/07/jdk-6-supports-in-classpath-but-be.html)
The very simplest way to do it is with the extensions mechanism:
java -Djava.ext.dirs=lib MyMainClass
For javac, the equivalent is the -extdirs flag:
javac -extdirs lib MyMainClass.java
It's not ideal - particularly if you also want to use the normal extensions - but it can be a useful little shortcut in some cases.

Categories