How do you pass multiple paths to Java -cp command? - java

So I am trying to compile a file that imports code from 2+ different .jar files.
The following is the command I am using to compile my file:
javac -cp /home/ugrads/majors/quinnliu/workspace/WalnutiQ/build/libs/WalnutiQ.jar:. HowMARK_II_FitsInToBrainAnatomy.java
Now I am getting an error because I am calling code in another .jar file in another folder but I don't know how to add it correctly to my current -cp command above.
Sample of errors I am getting:
HowMARK_II_FitsInToBrainAnatomy.java:3: error: package com.google.gson does not exist
import com.google.gson.Gson;

Use:
javac -cp jar1.jar;jar2.jar source1.java source2.java ...
On Windows you have to use semicolon to separate the JAR files, but on Unix you can use a colon.

Related

How to use javac with jars and packages

I'm trying to run java from the command line, and haven't had to include extra packages with the javac command before and I can't figure out what I'm doing wrong.
I'm running javac -d bin -cp jar1:jar2:...:jarN:PackageName MyClass.java but I'm still getting an error: package PackageName does not exist
I'm using the absolute paths for everything, and I also tried individually listing the java files inside the package but that didn't work either. I'm using a colon since I'm on a mac.
Anyone know what I am doing wrong? Thanks for any help!
I realized that I needed to compile the java files in PackageName before I could compile MyClass.java, which depended on them. So what I needed to do was: javac -d bin -cp jar1:jar2:...:jarN PackageName/*.java and then I could compile MyClass.java with bin added to the classpath as well as the jars.

Cannot run a .jar file

I'm having issues when I try to execute a .jar file. I always get the message from the command prompt "cannot find or load main class" every time I try to run the file I have just compiled.
I have tried the following(using the command prompt and saving the java code with Notepad++ and creating the manifest.txt file in the same folder where the .java code was saved):
javac classname.java
java cfm classname.jar manifest.txt classname.class
java -jar classname
I have also tried:
javac classname.java
java classname
And:
java packagename.classname
After compiling the .java file
I know there are hundreds of questions like this in the forums, but I have looked everywhere and I keep getting the same error. I just cannot run the .jar file even after I create it.
It's probably something I have missed but I am out of ideas.
Manifest:
Main-Class: classname
Let's see.
java -jar classname.jar
would work if your MANIFEST.MF calls out the correct class.
java -cp classname.jar my.package.MyClass
would work in any case.

Add jar files to classpath at launch time

I want to add some jar files to my binary at runtime, but I keep getting errors. I'm running this on a Windows machine. My code is in a directory called SeleniumTest.
Here is the command I used to compile:
javac SeleniumTest\src\com\src\test\First.java -d SeleniumTest\bin -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar
This worked successfully. However when I try to run this command:
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin com.src.test.First
I get a message:
Error: Could not find or load main class SeleniumTest\bin
My code, First.java exists in
SeleniumTest\bin\com\src\test
What am I doing wrong?
try this
java -cp "SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar;SeleniumTest\bin" com.src.test.First
try following
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin SeleniumTest\src\com\src\test\First

adding JAR class path in UBUNTU

This is might be a common question but I am not able to add class path for a JAR file in UBUNTU. I have given below all the details I know:
java is located here:
the o/p of which java command is - /usr/bin/java
sudo vim /etc/bash.bashrc
export CLASSPATH=$CLASSPATH:/downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar
ps: downloads folder is directly under the root
sudo vim /etc/environment
CLASSPATH="/usr/lib/jvm/jdk1.7.0/lib: /downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar:"
As you can see, I have added the class path in bashrc and etc/environment... but still I am getting an error while trying to run the S3Sample.java which comes with awssdk for java.
when I compile the java file, I get the following errors:
ubuntu#domU-12-31-39-03-31-91:/downloads/aws-java-sdk-1.3.24/samples/AmazonS3$ javac S3Sample.java
S3Sample.java:25: error: package com.amazonaws does not exist
import com.amazonaws.AmazonClientException;
Now, I clearly understand that the JAR file is not added to the class path and so I am not getting the error. I've also tried javac with the class path option - but it does not work :(
PS: JAVA home is set correctly as other java programs work properly.
To set the classpath, it is in most cases better to use the the -cp or -classpath argument when calling javac and java. It gives you more flexibility to use different classpaths for different java applications.
With the -cp and -classpath arguments your classpath can contain multiple jars and multiple locations separated with a : (colon)
javac -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass.java
java -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass
The classpath entry in the example sets the classpath to contain the current working directory (.), and the two jar files A.jar and B.jar.
If you want to use the CLASSPATH environment variable you can do
export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass

How to run Java program in command prompt

I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments

Categories

Resources