Java.lang.NoClassDefFoundError in command prompt run [duplicate] - java

This question already has answers here:
NoClassDefFoundError: wrong name
(8 answers)
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?
Error: Could not find or load main class hello
Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)

This type of error is due to the class not found in the Classpath during runtime but found during compile time.
Look to print 
System.getproperty("java.classpath")
which will print the classpath so you get an idea as to the actual runtime classpath.
Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.
directory_that_holds_package>java package_name.Class_name

First, I take a guess that your program could run smoothly in Eclipse and Idea, but it gives this error in command line.
Now, you should include your program's package in command line. If your program is like:
package firstprogram;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Then you should run java firstprogram.HelloWorld in FirstQuarter folder.

This error is mainly due to when the program is not able to access the class which you have defined in your program and it can be due to reasons like you have not defined the proper classpath or you have not included the required library needed to run that class. The reasons can be many.
So try to run your code on any IDE as you will be able to easily identify the errors.

Related

Using command prompt to execute .java

I am trying to use the command line to execute a java class but I am receiving the below error
"error could not find or load main class
caused by java.lang.noclassdeffounderror"
I could used the "javac" to create my java class but then I ma getting this error.
Thank you for any help,
Regards
package start;
public class sdz1 {
public static void main(String[] args) {
System.out.println("Hello World !");
}
}
I just found that is not working when I am in the src folder created by eclipse where my ".java" is located.
Anyone has a explanation why is not working in this case ?
Check you've all required classes present at runtime as those were available at compile time
NoClassDefFoundError :- comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time
Read more: https://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html#ixzz6VZaZBT8y
Check that you have written all the "public class..." and the main method. If you then compile it, it shouldn't give you errors.
If your program is in directory start, execute this from the directory just above that to compile:
javac start/*.java
and then execute this to run:
java start/sdz1

java program written in Vscode IDE having different package is not executing properly [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
I am getting the following error in VScode IDE .
Kindly help me with the Solutions related to VScode
I created a package named as test in visual studio code .
The program written in that package failed to execute
, program is :
package test;
public class Example{
public static void main(String args[])
{
System.out.println("HELLO");
}
}
It throws an error
the same program works completely fine when placed in the default package here it is
kindly help me with the package working or help me if there is any setting in vscode which has to be setup
The problem is that you cannot run the code from inside the package.
You will have to run the code from Parent Dir of package
EG. If your files,package is structured this way
SomeDirectory
|----test
│ |----Example.class
then you have run it from SomeDirectory by java test.Example

Command line finding errors that Netbeans does not [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
Edit: This is not a duplicate because the other thread contains solutions and suggestions based on correcting syntax within my code. My code compiles and runs just fine within an IDE, as I already stated in the original post. I am simply placing this statement at the top so that it will not be missed by do-gooders who get too excited about the chance to mark duplicates and forget to read content. Gosh I love stackexchange.
Good afternoon! I am trying to refresh my Java skills after a lot of years off, and I am following along with an online textbook.
The textbook suggested that readers get familiar with command line usage, which wasn't really emphasized in college. One of the tasks was to drag two java classes into any working directory, compile and run them with javac and java commands.
javac TextIO.java
works great.
javac NewWorld.java
produces 12 identical errors:
error: cannot find symbol
symbol: variable TextIO
location: class NewWorld
TextIO is the class with some subroutines that read and write to wherever. NewWorld is the main class with some little hello world type stuff that reads input and writes to a file with the TextIO class.
Help me help myself understand the command line environment. Let me know if you need more details!
What does a "Cannot find symbol" compilation error mean?
Possible issues:
Incorrectly spelled
Forgot to import
Forgot to declare something
etc.
The most possible case of this behavior I may think of is that you didn't set CLASSPATH Environment Variable in your operating system yet. When working with IDEs, like Eclipse, they take care of it and you don't need to worry about it. There are many tutorials online pointing how to correctly set a CLASSPATH in your operating system step by step, just google for setting CLASSPATH for JRE and you find how to set it step by step. You can set it through the proper commands in command line, here are some informations about how to set it properly:
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

Why does Java not compile my code? [duplicate]

This question already has answers here:
Java: Unresolved compilation problem
(10 answers)
Closed 4 years ago.
Language: Java
JRE: jre1.8.0_144
IDE: Eclipse Oxygen Release (4.7.0)
Description: After I hit the "run" botton, the code did not compile and red text appear in the console.
Screenshot:
Here is the code:
package chapter3.project2;
import java.util.*;
public class MetricConversion {
public static void main(String[] args) {
System.out.println("WTF Eclipse?");
}
}
error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at chapter3.project2.MetricConversion.main(MetricConversion.java:30)
It seems that no matter what I put in the main method, the error just kept popping up.
Thank you.
Where is jre library? Why not compile and run from source folder?
Check project property and add jre runtime library also create source folder (src) and compile and run.
Or
Build a clean Java project and add packages and classes in src folder.

Eclipse wanting to run wrong class [duplicate]

This question already has an answer here:
Eclipse is running the wrong class [closed]
(1 answer)
Closed 8 years ago.
I am a new java developer out here, and ive come across this weird thing, that eclipse still thinks my main class is called illuminati (just randomly wrote something..)
here is a screenshot of the error i get:
so what the problem is, basicly eclipse cant find the mainclass even though i changed the name..
and i dont know how to configure it that the class is gone (or changed name)
Thanks in advance!
Just select the class you want to run (MAIN), then click the green-arrow-in-circle Run icon, and select "Run As -> Java Application".
If you rename the class and you didn't let Eclipse rename the run configuration, then you can simply create a new run configuration for the new class.

Categories

Resources