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.
Related
I want to manually compile Java-Code with javac. I'm using multiple Images loaded with getClass().getClassLoader().getResource("...") If I run my Code in an IDE everything works fine. But when I'm trying to compile with javac no Resources are loaded. Is there a way to specify the Folder which contains all my Resources? I already tried --add-module but this didn't work either.
My command for compiling looks like the following: javac -d ./out main/ai/*.java main/logic/*.java main/gui/*.java main/network/*.java && java -classpath ./out logic.Launcher
The solution was to just include the path to my Resource Folder in the classpath. I just had to add -classpath path/to/my/res So my working command looks like this: javac -d ./out main/ai/*.java main/logic/*.java main/gui/*.java main/network/*.java && java -classpath ./out:./res logic.Launcher
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.
I know this question has been asked and answered a number of times. But I somehow am not able to get this right. I have a package having the following structure
model/
InputDetails.java
RelationDetails.java
Now the file RelationDetails has the following structure:
package model;
public class RelationDetails {
....
}
And the file InputDetails has the following structure
package model;
public class InputDetails {
.....
}
Now I have compiled the RelationDetails.java file that creates a RelationDetails.class file in the same directory.
But when I try to compile the InputDetails.java file, It shows the error
Symbol not found
wherever RelationDetails has been used. Where am I going wrong??
I'd recommend using an IDE like Eclipse or IntelliJ IDEA. They will do the compiling for you. Or use Ant, Gradle or Maven to compile. I am a professional Java developer and I cannot remember the last time I used javac from the command line. There's no need for it.
If you insist on using javac directly, either compile both files together from the appropriate source folder (the directory above "model").:
javac "model/InputDetails.java" "model/RelationDetails.java"
Or, if you want to compile them separately:
javac -classpath . "model/InputDetails.java"
javac -classpath . "model/RelationDetails.java"
The -classpath . bit adds the current folder to the classpath for the javac executable, so it can find the previously compiled class and you won't get the 'Symbol not found' errors.
$ pwd
/tmp/model
$ ls
InputDetails.java RelationDetails.java
$ javac InputDetails.java RelationDetails.java
$ ls *.class
InputDetails.class RelationDetails.class
I am just tried in my eclipse nothing will be showing errors, better to user Eclipse or STS they will help you like this problems easily I think so..
compile with fully qualifier name.
javac model\YourClass.java
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
My structure looks like this
\Project1
\src
\pkg1
Main.java
\pkg2
Auxillary.java
\Destination
\class
\lib
I need to compile Main.java which has dependencies in Auxillary.java and jars in \lib into \Destination\class
I am in the Project1 directory.
I tried
javac -cp Destination\lib\*;src\pkg2\* -d Destination\class
However, I get a package not found for Auxillary.java.
What am I doing wrong?
A classpath entry can't refer to a source directory. Try this:
javac -Djava.ext.dirs=Destination\lib -d Destination\class
src\pkg1\Main.java src\pkg2\Auxiliary.java
i.e. compile all the source code in one go. Alternatively:
javac -Djava.ext.dirs=Destination\lib -d Destination\class
src\pkg2\Auxiliary.java
javac -Djava.ext.dirs=Destination\lib -cp Destination\class
-d Destination\class src\pkg1\Main.java
That will compile Auxiliary.java first, and then use its destination directory as part of the classpath when compiling Main.java.
You can use ant script to make these steps simpler. Try once!