CLASSPATH set but java compiler cannot find class files - java

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.

Related

java -cp option seems not to work in Java 14 (Preview Enabled) [duplicate]

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.
I've tried:
]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main
etc...
Each gives an error saying:
Error: Could not find or load main class ....
or gives the NoClassDefFoundError indicating the libraries are not being found.
I even tried remaking the JAR file and included the lib directory and contents, but still no dice...
How can I execute a JAR file from the command line and specify the classpath to use?
When you specify -jar then the -cp parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
include all jar files from the lib directory into the manifest (you can use relative paths there)
Specify everything (including your jar) on the commandline using -cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
You can do these in unix shell:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
You can do these in windows powershell:
java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:
Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar
Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.
Then execute it with
java -jar MyJar.jar
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.

Compiling and running java program with other programs on build path

I'm having trouble getting my program to run in the command line. projectA has projectB and projectC on the build path.
I run this command:
javac -cp "projectB/src/":"projectC/src/" path/to/projectA/src/packagename/Program.java
This compiles fine. All the .java files in projectA, projectB, and projectC compile into .class files. However, when I run the command:
java -cp "projectB/src/":"projectC/src/" path/to/projectA/src/packagename/Program
I get the following error:
Error: Could not find or load main class path.to.projectA.src.packagename.Program
I have tried running the java command with many different derivatives of the -cp, paths, and from different working directories. Thanks for the help!
You need to have proper class path of projectA
java -cp "projectB/src/":"projectC/src/":"path/to/projectA/src" packagename.Program
With your command, javac thinks that the package is path/to/projectA/src/packagename/Program
You need to specify the fully qualified name (packagename.Program) and add the path in your classpath:
java -cp "projectB/src/":"projectC/src/":"path/to/projectA/src" packagename.Program

How to add external library in classpath?

I have a folder that contains these java files: Main, TableManager, CustomFileWriter, CustomFileReader plus the external library commons-lang3-3.0.jar.I'm trying to compile the Main.java with this command
javac -cp commons-lang3-3.0.jar Main.java
but it says cannot find symbol
TableManager table = new TableManager()
I create an instance of TableManager in Main class. Without the external library and compiling with just javac Main.java works fine. How can I fix this. I need the external library for the StringUtils. I'm not using frameworks. Just text editor and running to terminal.
To compile a Java file and include a Jar file, enter the following command line:
javac -cp jar-file Main.java
For multiple JAR files, separate the jar-files with semicolons ;, with the following command line:
javac -cp jar-file1;jar-file2;jar-file3 Main.java
You need the path, not just the jar name, like
javac -cp c:\home\ann\public_html\classes\compute.jar
engine\ComputeEngine.java
You can check it in the documentation.
To compile a class (on windows) with a jar in the same direcory use:
javac -cp .;myjar.jar MyClass.java
To then run the class you can use:
java -cp .;myjar.jar MyClass
NOTE: on linux you will need to replace ; with :
The "-cp" Option overwrites your classpath. So in order to successfully compile and run your java-app, you have to add the path of your Main.class file and the external library as arguments. Here the "." is the relative path to your Main.class file and commons-lang3-3.0.jar the relative path to the external library. Under Windows it is sometimes necessarry to use quotes.
To compile:
javac -cp ".;commons-lang3-3.0.jar" Main.java
To run:
java -cp ".;commons-lang3-3.0.jar" Main

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

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.

Categories