I have downloaded whole java 3 times and still get this error after executing in cmd
enter image description here
Make sure to compile code with exactly the same version of Java you are trying to use.
To do so, make sure that both:
java -version
javac -version
produce the same version information.
Let's say you have file: Hello.java
public class Hello {
public static void main(String [] arg) {
System.out.println("Hello world!\n");
}
}
Make sure to compile it:
java Hello.java
and then, run it
javac Hello
Your %PATH% variable should point to very same JDK and JRE if you want to compile and run the code.
in cmd, type where java, remove all other java path from environment variable other than the one from jdk directory
Related
If I remember correctly
javac filename.java -> compile and generates classname.class(es)
java classname without .class extension
But when I try java filename.java executes successfully while java classname command gives the following error ,
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}
javap HelloWorld.class is giving below output
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
public static void main(java.lang.String[]);
}
java HelloWorld.java -> executes fine, no class file generated.
java HelloWorld -> didn't execute.
Any idea why the program is behaving like this?
After some help from some stackoverflow veterans in the comment section I was able to understand what went wrong.
The latest version of Java have introduced launching single file source code directly using Java command.
from oracle docs.
To launch a single source-file program:
java [options] source-file [args ...]
To run Helloworld.java, you can directly call execute java Helloworld.java it will execute the java program and gives output without generating .class file in the current directory.
Why old way of running java class file didn't work for me?
I had a class path variable 'CLASSPATH' in my environment, so when I execute java HelloWorld it is not looking for class in current directory. Give java -cp . to explicitly give current directory to classpath.
java -cp . HelloWorld
Credits: Jon Skeet,Joachim Sauer, rzwitserloot
I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214
The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.
I have recently installed canopy (i don't know if this has anything to do with java)
i have set to system variables the path to javac and java
i was writing compiling and running java programs normally until now that it keeps telling me it can not find or load main class.class
example of code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
what is happening?
ok i changed hello to Hello
i run at powershell:
javac Hello.java
java Hello.class ( i am in the directory of the file)
Error: Could not find or load main class Hello.class
You need to run your program using
java Hello
and omit the .class extension
That should compile fine which Java compiler are you using? Make sure to always save and compile your program before running it.
I'm trying to compile my programs using the command prompt on Windows 7. I'm having a problem when I during compile. I created a test program:
class test
{
public static void main (String args[])
{
System.out.println("this is working!!!!!!");
}
}
I use the following instructions from the command line:
cd \
cd summer
Javac test.java
java test
I have even tried using javaw and I get the same message but in a pop-up box
Could not find the main class, program will exit.
Any suggestions?
It seems your JDK and JVM have different editions.
You are using different versions of JDK. Check the versions on your javac vs java. This error is telling you that your java and compiled class are not same versions. Check your path for configuration, type "set" in dos to see details.
Example: If you compiled your class with javac (version 7) and execute it with java (version 6)