Setting Javac classpath - java

In the dev/proj/app/test.java file I have:
package proj.app;
...
import lib.sec.x;
...
In the /classes/proj/lib/sec I have x.class file.
When I try to compile java:
javac -classpath "/classes/proj/*" test.java
I got this error message:
package lib.sec does not exist
How to reference x.class file within classpath?

The Java package is part of the (fully-qualified) class name. This
javac -classpath "/classes/proj/*" test.java
should be something like
javac -classpath "/classes:/classes/proj" proj/app/test.java
But your really shouldn't put lib (if it's a package name) under proj (a different package), the lib folder should be moved under classes. And then you only need /classes in the classpath.

Related

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

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.

Unable to get javac to automatically recompile a source file

I am experimenting with the javac command line options, in order to learn about the -sourcepath and -classpath options. When I run javac, having tried four different command line options for it, I am unable to obtain a recompiled .class file.
Here is my folder structure. Please note that due to testing, the Test.java file is located inside the "bat" folder, which is an admittedly odd location.
projects \ prj1 \ bat \ bat.bat
Test.java
Test.class <--- unable to obtain recompiled file.
src \ Main.java
Main.class
The contents of my two .java test files are:
// Main.java, located in the src folder
class Main {
public static void main(String[] args) {
new Test();
}
}
// Test.java, located in the bat folder
class Test {}
Regarding the execution of javac at the command prompt, here are four options that I have tried. I've run these commands from a batch file called bat.bat, which is located inside the "bat" folder.
javac ..\src\Main.java (no sourcepath, no cp)
javac -sourcepath . ..\src\Main.java (sourcepath)
javac -cp . ..\src\Main.java (cp)
javac -sourcepath . -cp . ..\src\Main.java (sourcepath, cp)
In all of these javac commands above, I am unable to obtain a recompiled .class file, for the Test.java file. Is this because I have not edited the Test.java file, since initially compiling it? Please note that I have no CLASSPATH environment variable set. Thanks.
If class files are already present in the destination / output folder, javac will only recompile the source java file if it has been modified since the date/time of the class file.
If you want to recompile the source files, then first delete the *.class files before calling javac.

Unable to compile Java class because the file isn't found

I have file under classes in tomcat...\webapps.....\classes named PropertyExample.java
in classes I have a folder called foo in which I have a class person.java
I am importing that person file in PropertyExample.java
and trying to compile PropertyExample.java this below but its showing an error
C:\>javac -cp .;"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes" PropertyExample.java
javac: file not found: PropertyExample.java
Usage: javac <options> <source files>
use -help for a list of possible options
You execute javac the the folder C:\ (root folder). The .java file is somewhere else.
So it can't be found.
The command: C:\javac Someclass.java works IF and ONLY IF the file Someclass.java is in the folder C:\ you wrote that your file is in ...tomcat/webapps/classes (whatever) so you must do a cd to that dir before calling the javac. like this:
cd c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes
javac -cp . PropertyExample.java
The classpath tells javac where to find class files - not where to find source files. You need to give the path name to your source file (either relative or absolute). For example:
javac -cp "c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes"
"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes\PropertyExample.java"
Of course it would be easier just to change to that directory to start with... or better, to use an IDE or a build system like Ant...

Having a trouble on java.lang.NoClassDefFoundError

I have a test.jar package and a Test.java file in the same folder(it is not in my CLASSPATH).
In the .java file I import some classes that are in the .jar package.
So I go the that folder, use java -classpath test.jar Test.java to compile. Everything looks good. Then I use java Test, it gives me an error:
Exception in thread "main" java.lang.NoClassDefFoundError:......
saying that it cannot find the classes inside the .jar file.
I move the file to Eclipse and add the jar file, it works.
just as you compiled the code using javac -classpath test.jar Test.java you should use the java command to run (add the jar file in the classpath)
java -classpath test.jar Test.java
You must run the java command with the classpath too, same as the compiler.

How can I compile my Java class depending on another class and a some libraries?

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!

Categories

Resources