so I've tried many of the solutions that are present in this website but none could help me.
The problem:
I have my project files structured like this:
cnv/webserver/aplication/insProj.java
cnv/webserver/aplication/IntFactorization.java
and both are in the package webserver.aplication.
to compile and run i do this:
starting at the folder cnv and with the $CLASSPATH=/home/ll/Documents/cnv
cd webserver/aplication
javac -source 1.4 insProj.java
javac IntFactorization.java
cd ..
cd ..
java -XX:-UseSplitVerifier webserver.aplication.insProj ./webserver/aplication/IntFactorization.class
java -XX:-UseSplitVerifier webserver.aplication.IntFactorization 5
When executing this last instruction, it returns an exception:
Factoring 5...
Exception in thread "main" java.lang.NoClassDefFoundError: webserver.aplication.insProj
at webserver.aplication.IntFactorization.calcPrimeFactors(IntFactorization.java:22)
at webserver.aplication.IntFactorization.main(IntFactorization.java:59)
The instrumentations i'm making is incrementing a counter when a given method is executed and creating a file when the class ends execution. I know this works because i've tested outside these packages and it works fine.
It seems BIT doesn't work well with packages. In the end, I removed insProj from any package and inserted its folder in the classpath. Then it began to work.
Related
Problem explanation
I have recently been trying to use Apache Jena with Java (rather than on the command line). I wrote a simple script to convert read and write differetn RDF format types, as so
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.query.Dataset;
import org.apache.jena.riot.Lang;
public class Go_NT
{
public static void main(String[] args)
{
Dataset dataset = RDFDataMgr.loadDataset("triail.nq");
RDFDataMgr.write(System.out, dataset, Lang.NTRIPLES);
}
}
triail.nq is a test nquads file containing 81 quads.
I invoked it as so:
javac -cp "/mnt/e/Tráchtas/apache-jena-3.17.0/lib/*" Go_NT.java
java Go_NT
It compiles without error, but when I run it, it returns an error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/riot/RDFDataMgr
at Go_NT.main(Go_NT.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.riot.RDFDataMgr
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
What I have tried
I have looked around and seen that this error occurs almost always because a necessary .jar file is not included, so a class referenced by the code cannot be loaded. The solution to these other issues was to include all of /apache-jena-3.17.0/lib/* . Oddly enough, that has not worked for me--I do include all of the contents of lib/ in my classpath, but I am still seeing the error.
System notes
I am running Jena 3.17.0, using the default Linux binaries available here (https://jena.apache.org/download/index.cgi). I have not added or removed any other Jena modules.
I am running this in the Windows Subsystem for Linux (version 2) with Ubuntu 20.04.
If any of you have any insight into what could be causing this, I would greatly appreciate it!
Based on the comment by vvs, the link https://howtodoinjava.com/java-examples/set-classpath-command-line/ helped out a lot. There were 2 issues: I needed to include the classpath in the java command, not just javac. I also needed to include the current directory where the output of javac would be.
I fixed this by setting the CLASSPATH variable, and then adding all the needed directories to that. (You could also do this by adding the classpath into the -cp argument). Note that the : separates different directories.
In short, here is what I did:
export CLASSPATH=/mnt/e/Tráchtas/apache-jena-3.17.0/lib/*:.
javac Go_NT.java
java Go_NT
Note that you need to re-assign CLASSPATH each time you open a new terminal.
I am a new beginner of Java. I wrote a Hello.java file with a extend jar package "algs4.jar"(my textbook asked me to use their own output/input library(Algorithm 4th Edition)), which can print a 'Hello,World!\n' string on the shell when executed.
It was compiled without any error, (command:javac -cp E:\AlgorithmExercise\lib\algs4.jar E:\AlgorithmExercise\codes\Hello\Hello.java)
but when I tried to run the class (command: java codes.Hello.Hello), I got this error.
I guessed the reason of this error is because I forgot to enter the path of the algs4.jar package. so I modified the command: java -cp .;E:\AlgorithmExercise\lib\algs4.jar codes.Hello.Hello, but the terminal informed me that it "cannot find or load the main class."
I am absolutely cannot understand what's going on, my classpath has no mistake on configure.
I want to run a java project in terminal. When I compiled, no error occurred, but when I run the program I get the following error:
Could not find or load main class orException in thread "main"
java.lang.NoClassDefFoundError: Appium (wrong name:
com/appiumproj/test/Appium)
Please help me to solve this problem.
iMac:~ Samuel$ javac -cp /Users/Samuel/Downloads/AppiumTest/lib/selenium-server-standalone-2.45.0.jar:/Users/Samuel/Downloads/AppiumTest/lib/gson-2.3.1.jar:/Users/Samuel/Downloads/AppiumTest/lib/java-client-2.2.0.jar: /Users/Samuel/Downloads/AppiumTest/src/com/appiumproj/test/Appium.java
iMac:~ Samuel$ java -cp /Users/Samuel/Downloads/AppiumTest/lib/selenium-server-standalone-2.45.0.jar:/Users/Samuel/Downloads/AppiumTest/lib/gson-2.3.1.jar:/Users/Samuel/Downloads/AppiumTest/lib/java-client-2.2.0.jar: /Users/Samuel/Downloads/AppiumTest/src/com/appiumproj/test/Appium
Error: Could not find or load main class .Users.Samuel.Downloads.AppiumTest.src.com.appiumproj.test.Appium
iMac:~ Samuel$
You need to specify the name of the class - not a filename. It needs to be the fully-qualified class name, and it needs to be on the classpath. So after compiling, you'd want something like this (just spread out on multiple lines for readability; the backslashes are line continuations - you should be able to copy and paste this straight into your shell):
java -cp /Users/Samuel/Downloads/AppiumTest/lib/selenium-server-standalone-2.45.0.jar\
:/Users/Samuel/Downloads/AppiumTest/lib/gson-2.3.1.jar\
:/Users/Samuel/Downloads/AppiumTest/lib/java-client-2.2.0.jar\
:/Users/Samuel/Downloads/AppiumTest/src \
com.appiumproj.test.Appium
Are you sure your compiled version is in /Users/Samuel/Downloads/AppiumTest/src/com/appiumproj/test/ ? I would say that it is probably where the javac was run. Check and find it and specify the path to to compile version
This question already has answers here:
Exception in thread "main" java.lang.NoClassDefFoundError: wrong name
(3 answers)
Closed 9 years ago.
I have been fighting with this program for a little while now and cannot figure out what is wrong. Any help would be greatly appreciated.
So here's the issue. I have three classes one is for logging onto a mysql database, the other is to output data from the database, and the last one holds method main. I was having a huge issue with getting them to compile getting errors about not finding a symbol for a method in a different class. I finally got them to all compile by using command "javac -d bin/cdtPack src/CDT.java src/login.java src/ClientBase.java"
But, now when I try to run the class with method main I get error:
Exception in thread "main" java.lang.noClassDefFoundError: CDT (wrong
name: cdtPack/CDT)
then a list of at java....
Anyone have an idea what could be going wrong?
As the linked Q&A explains this happens when you try to run a Java application using the wrong class name.
Your class looks something like this:
package cdtPack;
public class CDT {
....
}
That means that its class name is "cdtpack.CDT".
But you are running it like this:
$ java CDT
The JVM is telling you this:
"You told me to run CDT, but when I looked at it, the class said its name is "cdtpack.CDT"!!"
You have to get your head around the way that the Java classpath works, and the way that javac and java and all of the other Java tools find classes.
Your "CDT.class" file should be in a directory called "cdtpack", and then "cdtpack"'s parent directory should be on the classpath; i.e.
Compile like this:
$ javac -d bin -classpath bin src/cdtpack/CDT.java
which should create "bin/cdtpack/CDT.class". Then run like this:
$ java -classpath bin cdtpack.CDT
It looks like CDT should either belong in the package cdtPack or you are running it from the wrong directory...
Try changing into the bin directory and run the class file again. Don't forget to include the package name before the class name. For example:
...\bin> java cdtPack.CDT
try this
you goto src directory and if class file available there, then type
java CDT
if the class is present in some other directory then type
java a/b/c/JavaClassName
if you want to add some runtime jar while running then
java -cp classpath=%classpath%;jarfilename.jar; a/b/c/JavaClassName
I have a problem while trying executing my java application.
Whenever I try to execute the program through the command
java ProgAudioJ
I get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
at java.lang.ClassLoader.defineClass1(NativeMethod)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(NativeMethod)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ProgAudioJ. Program will exit.
If I remove from my code:
package es_2011;
Everything works perfectly. How do I solve the problem?
Because I found these answers unclear, here is what you need to do.
First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):
cd /myFolder/myPackage
javac MyClass.java
cd ..
java myPackage.MyClass
Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like
package com.somedomain.someproject;
you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):
cd /myCode
java com.somedomain.someproject.MyMainClass
Try using:
java es_2011.ProgAudioJ
(instead of java ProgAudioJ).
I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.
Try this (compile and run):
dir
2011-02-10 00:30 <DIR> .
2011-02-10 00:30 <DIR> ..
2011-02-10 00:27 58 es_2011
javac es_2011/ProgAudioJ
java es_2011.ProgAudioJ
It's quite clearly stated there:
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,
src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;
(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.
Try this,
Compile your class using below command
$ javac ProgAudioJ.java -d .
Run your application by command
$ java es_2011.ProgAudioJ
The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.
I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file.
It has been packaged up as
es_2011/ProgAudioJ
In order to run it - you have to either move up a directory:
If you are here: (Windows)
src\es_2011\
move to
src\
Then run the line:
java es_2011.ProgAudioJ
This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).