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.
Related
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.
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 am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp
I have a java project that has 10 .java files. A1.java, A2.java......A9.java and Main.java.
The project contains a package grand.big.medium.small and all the .java files are inside this package.
I have compiled and created an executable jar file for the project using:
javac grand/big/medium/small/*.java
to run the project I used the command
java -cp . grand/big/medium/small/Main
to create the jar I used the command
jar -cvmf manifest.txt MyJAR.jar *.class
I want to run the jar file from command prompt and I am using the commad
java -cp . -jar grand/big/medium/small/MyJAR.jar
and I getting an error:
Error: Could not find or load main class Main
please can anyone tell me why I am getting this error?
Run as:
java -cp . -jar grand/big/medium/small/MyJAR.jar grand.big.medium.small.Main
Assuming you have grand as a directory within current directory and directory hierarchy follows.
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.