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
Related
I'm on Ubuntu 22.04 trying to run my code in the terminal.
The program works without problems in VScode, also when running multiple instances.
The program is consisted of six class files.
The trouble occurs when I try and run it with terminal. When compiling the java file
with javac it shows errors at places where I use the external libraries.
If I compile it with VScode and run the class file in terminal, I get the following error java.lang.ClassNotFoundException
This is causing me problems since I'm also supposed to dockerize the program.
You can add the following code in your setting.json file "java.project.outputPath": "bin",
This will be the .class file generated by VS Code in the bin folder of the same directory when running the Java code.
You can use the java command after entering the file directory with the cd command.
This generally indicates that the class path with which you're compiling your program does not include the correct paths to your libraries. Assuming your libraries are jar files, your javac command should look something like this:
javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java
where libs/ is the relative path to your libraries and srcs/ is the relative path to your own java files.
And when you run the program, make sure your class path includes both the locations of your libraries and the location of your class files (which in this case would be the current directory):
java -cp .:libs/lib1.jar:libs/lib2.jar <MainClass>
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
I am having trouble running a JAR file from terminal which has both native and .jar dependencies. Okay, my goal isn't to run it from the terminal, but to run it as a separate process with Java's Runtime.getRuntime().exec function, but if I can't run it from the terminal, then I also can't run it via. The JAR file I am trying to run depends on a number of other jar files as well as a number of .so libraries. I'm trying to put put all the .jar dependencies and .so dependencies in their own folders and then run the jar file with:
java -cp /home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/* -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/* -jar /home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar
Where "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/" contains all the JAR files and "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/" contains all the .so files and the main JAR file to run is "/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar", but I keep getting this error:
Error: Could not find or load main class
.home.johnmichaelreed.Desktop.Dropbox.Libjitsi_linux_64.some-compressed-jar-file.jar
Where some-compressed-jar-file.jar is one of the .jar files that my application is supposed to use.
Here's my Java JAR dependencies folder:
And here's my native libraries dependencies folder:
UPDATE:
Okay, this is the solution:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main
With attempt at command line args:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main "arg"
You can't use -jar and -cp at the same time.
What you can do, is adding your jar to the classpath and then specify your Main class to run. You can also specify the jar dependencies within the manifest of your jar.
Please have a look here for more details.
Assuming your Main class is in called Main and in the package foo.bar, then a possible call may look like:
java -cp "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*;/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar" -Djava.library.path="/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/*" foo.bar.Main
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.
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.