r/javahelp • u/TalxTrapi • May 12 '19
I can't execute java code after compiling
Hi there
Is there anyone that have come across this error:
Error:Could not find or load main class
Caused by: java.lang.ClassNotFoundException
I deleted and added the classpath reinstalled java but nothing has helped
The code compiles but it doesn't run
2
u/shagieIsMe Extreme Brewer May 12 '19
We really need to see what the name of the class is, what the files in the directory are and how you are running the programs.
It is probably something rather simple.
What we are expecting is something that looks like:
/tm/Hello $ cat Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
/tm/Hello $ javac Main.java
/tm/Hello $ ls
Main.class Main.java
/tm/Hello $ java Main
Hello World
/tm/Hello $ java NotFound
Error: Could not find or load main class NotFound
/tm/Hello $ Without that sort of information, we're only guessing as to why you are having trouble running Java.
The hello world instructions from Oracle can be found at https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
If you are having trouble with that, please identify exactly which step things go wrong along with what you currently have for code and where the files are located.
1
u/TalxTrapi May 12 '19
public class Test{
public static void main (String [] args) {
System.out.println("Test message");}
}C:\Users\taula\Desktop>javac Test.java
C:\Users\taula\Desktop>java Test
Error: Could not find or load main class Test
Caused by: java.lang.ClassNotFoundException: Test
2
u/shagieIsMe Extreme Brewer May 12 '19
From the link above, try:
Run the Program
In the same directory, enter the following command at the prompt:
java -cp . HelloWorldApp
You should see the following on your screen:C:\myapplication>java -cp . HelloWorldAppHello World!Also check out https://docs.oracle.com/javase/tutorial/getStarted/problems/index.html
1
u/TalxTrapi May 12 '19
Why did this work
3
u/shagieIsMe Extreme Brewer May 12 '19
Apparently the class path was getting overriden somewhere to not include
.by default. If you have a JAVA_CLASSPATH environment variable set, this could do it. By specifying it on the command line, it ignores the environment variable.1
u/TalxTrapi May 12 '19
Is there anyway i can that the class path to don't get overriden
2
u/shagieIsMe Extreme Brewer May 12 '19
I deleted and added the classpath
You probably did something there.
1
u/morhp Professional Developer May 13 '19
JAVA_CLASSPATH isn't set by default so you must have made a setting that changes it. Try
echo %JAVA_CLASSPATH%in the cmd window to see how it is set. Maybe that helps.1
u/plsdontattackmeok Nooblet Brewer Aug 29 '19
How about for MacOS?
1
u/morhp Professional Developer Aug 29 '19
I believe that should also apply to osx, but I don't use them, so no guarantee.
Edit: It's probably
echo $JAVA_CLASSPATHon mac and linux1
u/plsdontattackmeok Nooblet Brewer Aug 29 '19
Ahhh ok thanks. I also have same problem by OP too
but when I try echo $JAVA_CLASSPATH, I got blank instead
→ More replies (0)
1
u/philipwhiuk Employed Java Developer May 12 '19
How are you trying to run it?
1
u/TalxTrapi May 12 '19
In terminal Javac (name of class).java And Java (name of class)
1
u/philipwhiuk Employed Java Developer May 12 '19
Is your class in a package.
1
u/TalxTrapi May 12 '19
Nope
2
u/morhp Professional Developer May 12 '19
Show us your code. And maybe a screenshot of your file manager so we can see the files.
1
u/TalxTrapi May 12 '19
A screenshot with code and the terminal http://prntscr.com/nnmgvf
I don't think that the problem is in the code1
u/morhp Professional Developer May 12 '19
That looks fine. Make sure the Test.java in Notepad is saved and you opened the right file in the right Desktop folder. Must be a dumb mistake like that.
3
u/desrtfx Out of Coffee error - System halted May 12 '19
Is your
.javafile (and consecutively, your.classfile) named exactly the same as the class that contains thepublic static void main(String[] args)method (including spelling and capitalization)? Is the classpublic?You're not giving us sufficient information to be able to help.