Java.lang.classnotfoundexception - HelloWorld.class [duplicate] - java

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
Can't seem to get my head around what is causing this error. I have set the CLASSPATH in Environment Variables to C:\Program Files\Java\jdk-10.0.2\bin.
I can compile the code into a .class file using javac HelloWorld.java. However when trying to run the .class file using java HelloWorld, I am getting the below error:
I am running the code from C:\Java which is the directory of both my .java and .class file.
c:\Java>java HelloWorld
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
CODE:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Any tips would be greatly appreciated.

The CLASSPATH environment variable is not supposed to point the location of your java installation (you don't really need any environment variable to point at that. A few outdated tools MIGHT need you to set JAVA_HOME, but not to the 'bin' dir but to its parent).
It is supposed to point to the location(s) of your class files.
If your HelloWorld.class file has no package declaration and is located at C:\java\HelloWorld.class, then C:\java needs to be your classpath.
You can use CLASSPATH for this, but... don't. You can have multiple projects on a machine so the notion of 'one machine, one classpath' is silly. Use command line params:
java -cp c:\java HelloWorld

if your classpath,
CLASSPATH=C:\Program Files\Java\jdk-10.0.2\bin
Your class loader will look .class files from there,
include your current directory into your CLASSPATH, where as in your case your .class files are at C:\Java, so the java could not find your .class file, try this one
CLASSPATH=C:\Java
CLASSPATH variable is where java looks for .class and jar file paths
PATH and CLASSPATH

Related

Compiling java class from command line defined in a package [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I'm able to compile and run a simple hello world program from command line with
javac hello.java
java hello
however, if I had a package statement at the top package com.mypackage.myclass and try the same I get
Error: Could not find or load main class Reflection
What is exactly happening? And how do I fix it? Thanks.
UPDATE: Thanks everyone. I already had created the directory structure manually. In order for this to work I have to run java com.mypackage.myclass from the root directory, otherwise it will not work. Still don't understand the underlying mechanism. What is exactly happening?
If you have a java class like com.example.test.Main then the compiled class should be in a folder com\example\test and the class file name Main.class
You could manually do it, but compiler has a switch -d <target folder> which will create the folder structure for you.
javac -d . Main.java
In the above command the target folder is the current folder where you have the Java source file.
Once compiled you must use fully qualified name of the class to execute from the same folder you compiled the java file.
java com.example.test.Main
Screen Cap

Running a java program with multiple jar files and classes [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

What does "Exception in thread \"main\" java.lang.NoClassDefFoundError" mean when executing java .class file?

Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.

"Could Not Find or Load Main Class..."

I am new to Java and I got this book to help me start.
I have successfully compiled Hello.java using "javac Hello.java".
Now it says to type in "java Hello" and I am getting "Could not load of find main class Hello". I have tried to find out how to fix it before but all the answers are complicated and confusing. If someone could explain how to fix this, that would be awesome.
The problem appears to be of CLASSPATH.
Solution 1
Add the path of your directory where you have compiled your class to the CLASSPATH variable in Environment Variables.
Solution 2
Every time you run the program add the current folder and libraries you are referencing in the classpath using -classpath. eg:
java -classpath .;lib/referenced-libs.jar my.package.MainClass
Make sure the main method with exactly this syntax is present in the Hello.java file:
public static void main(String[] args) {
// your code will go here...
}
you should check if the hello.class file is present in the folder. java runs these .class files. These files are created on successful compilation.

default classpath current directory anomaly

I am trying to compile and run simple Java program. This program basically prints out hello world phrase. I am not specifying -cp option and I don't have CLASSPATH environment variable. Hence the user classpath is limited only to current directory.
Now, compilation works beautifully.
rustam#rustam-laptop:~/temp/bird_test$ javac Sparrow.java
This command produces needed .class file. The weird stuff happens when I try to run .class file. The following command works good.
rustam#rustam-laptop:~/temp/bird_test$ java Sparrow
But when I try the following command
rustam#rustam-laptop:~/temp/bird_test$ java ./Sparrow
I receive the following error:
Error: Could not find or load main class ..Sparrow
WTF! i thought that symbol ./ refers to current directory.
java takes a class name as argument. It doesn't take a file path. The class name (Sparrow) is then resolved by the java class loader to a .class file based on the classpath, i.e. it looks for a Sparrow.class file in every directory and jar listed in the classpath.
Let's take an example that respects good practices, and thus doesn't use the default package:
package foo.bar;
public class Baz {
...
}
The class name of the above class is foo.bar.Baz. To execute it, you must use
java foo.bar.Baz
and java will look for a foo/bar/Baz.class in all the directories listed in the classpath. So if the classpath is set to /hello/world, it will look for the file /hello/world/foo/bar/Baz.class.

Categories

Resources