Getting error "cannot find or load main class HelloWorld" - java

I have this simple code:
public class HelloWorld{
public static void main(String[] args){
System.out.println("HelloWorld");
}
}
And the filename HelloWorld.java
In command prompt, I type in:
javac HelloWorld.java
java HelloWorld
(same directory)
I am getting the error: "cannot find or load main class HelloWorld"
I'm sure it has nothing to do with improper installation because I reinstalled jdk and jre twice.
Edit:
This was working before, and the next day, no change of code, directory, or anything, its started giving an error.

You could get this behaviour if you have an incorrect / inappropriate setting of the CLASSPATH environment variable; e.g. the current directory isn't on the classpath. (It is by default ... )
Try this:
java -classpath . HelloWorld
Assuming that works ... the problem is your understanding of the concept of the "classpath". This is explained well by the Oracle documentation:
The java & javac command documentation ... particular the -classpath argument
Setting the Classpath.
The Java Tutorial - PATH and CLASSPATH

try:
java -cp "C:\WhatEverDirectoryYourFileIsIn" HelloWorld

In CMD, instead of typing:
java HelloWorld
Try typing:
java HelloWorld.class

Related

Could Not Load Main Class (Java)

Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo

Java compiles my program, but I cannot run it

I want to run a Java file with the following source code:
package u0a1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run the file I did the following things:
C:\.. \u0\u0a1> javac HelloWorld.java (this works, class file is created)
Then I try to run it with:
C:\..\u0> java u0a1.HelloWorld
This step doesn't work. Main class could not be found.
I also tried
C:\..\u0\u0a1> java HelloWorld
C:\..\u0> java u0a1\HelloWorld
None of them worked.
This is a piece i found somewhere else, worked for me.
Have you set your JAVA_HOME correctly? If not you have to work with
the full path
Example: "C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe"
HelloWorld.java
If you have runtime issues, you should work it out like this
Select MAIN directory - not package directory
java u0a1/HelloWorld
If you have problems with CLASSPATH or JAVA_HOME - try this:
"C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe" HelloWorld.java
source: http://quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd
You are compiling a program of package , so it should compile this way
C:.. \u0\u0a1> javac -d . HelloWorld.java
here -d for creating package u0a1
and "."
for from current working directory
after compiling this way a folder will create with name "u0a1"
then other thing will work properly

Java HelloWorld program run

I tried to run my basic HelloWorld.class file from my terminal.
I use the following input:
Java HelloWorld.class
But it says:
Error: Could not find or load "HelloWorld.class"
I have tried giving it a directory but it doesn't work.
because you didn't compiled or run it successufully.you should use
javac HelloWorld.java
to complile and
then use
java HelloWorld
to run it.
plz check this tutorial http://introcs.cs.princeton.edu/java/11hello/
The class should be (Executable Class should definitely contain the main method with same declaration as below)
Class MyClassName
{
// Methods here
public static void main (String args[])
{
// Code here
}
}
To Compile, it should be:
javac MyClassName.java
On successful compilation, MyClassName.class would be generated in your folder.
To run, it should be
java MyClassName
In case your java is in say D:/JavaWorkDir/src, You need to compile and run from the folder D:/JavaWorkDir/src. Also Ensure that your classpath is set appropiately.
You don't run it as
java HelloWorld.class
but
java HelloWorld
There is no need of .class extension.
But note you always have to use fully qualified name. So if your class resides in some package say myPackage then you need to run
java myPackage.HelloWorld
You are receiving this error because you shouldn't include the .class when you run the compiled file.
After you've compiled:
javac HelloWorld.java
run using:
java HelloWorld
(don't do: java HelloWorld.class)
Run it as Java HelloWorld and not like Java HelloWorld.class.
The error Error: Could not find or load "HelloWorld.class" is occuring because:
The class may have not compiled correctly.
The compiled class is not available on the path from where you are trying to run it.
Proper classpath is not set for compiling and running the java classes.
Whenever you write a Java Program named HelloWorld, you must compile it as:
javac HelloWorld.java
Once the HelloWorld.class class file generated in the same directory where you have your java file, compiled by the compiler, you can run it from console as:
java HelloWorld
if u want to try hello world you can also try to run it on the NetBeans application and also on Jdoodle.com

How to execute my HelloWorld script

I'm currently trying to run my first java script:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I decided i'd take a little look into Java. However I come from languages like JavaScript and PHP which don't require any compiling or anything as such.
So far, I think i'm compiling it correctly in the command prompt:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" "HelloWorld.java"
It adds a file called: HelloWorld.class so figured I did something right.
However, now when I try to actually run the program using:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "C:\Users\Shawn\HelloWorld.class"
I get this, Error: Could not find or load main class C:\Users\Shawn\HelloWorld.class.
However, if I try that same command but use javac.exe instead I get:
javac: invalid flag: C:\Users\Shawn\HelloWorld.class
Usage: javac <options> <source files>
use -help for a list of possible options
Why is this happening? Why isn't my program executing correctly?
The java command takes the name of the class, not the name of the file.
It then uses the Java class loader to find the .class file for that class in the current directory or the class path.
When you pass HelloWorld.class, it looks for a class named class in the package HelloWorld.
(that would be ./HelloWorld/class.class)
You need to pass HelloWorld.
As others have pointed out the argument needs to be just the class name, without the extension .class. There is a second requirement that must be met, though: the class file must be in the classpath.
It's usually not advisable (although convenient) to include the current directory in the global classpath, but you can override it on the command line:
java -cp . HelloWorld
You can also specify an explicit path:
java -cp "C:\Users\Shawn" HelloWorld
If your Java program uses other classes, include the global classpath like this:
java -cp "%CLASSPATH%;." HelloWorld
See the following javac documentation for more info. especially the section on Cross-Compilation Options.

Running a Java Class in Ubuntu with Virtual Box

Since I'm quite new in Java i have a question. As told on the title , I run an Ubuntu Server on VirtualBox and I have a problem running a very simple class with the use of package.
I give you the code:
package world;
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World")
}
}
Very simple code indeed. After compiling it with javac HelloWorld.java, with no mistakes (ok now what mistakes could possible find),
Running java HelloWorld, gives me the message NoClassDefFoundError
Running java world.HelloWorld returns cannot find or load main class.
I suspect that it has something to do with classpath , but I cannot find an answer.
It's a classpath issue. You can probably check to see what your classpath is by looking at the CLASSPATH environment variable. You can try adding the directory your classfiles are at to the end of this CLASSPATH, but the simplest thing to do is probably the following.
Make sure the HelloWorld.java file is in a directory called world, and you can compile is like:
javac world/HelloWorld.java
This will create a HelloWorld.class file in the world directory. You can then try running
java world.HelloWorld
or
java -classpath . world.HelloWorld
from the same place.
You can also use the -d flag with javac to put the class files in a different place instead of the same place the source (.java files) are.

Categories

Resources