• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

couldn't find class Main.java

 
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Users\Eli\Documents\Programming\Java>javac Main.java

C:\Users\Eli\Documents\Programming\Java>dir
Volume in drive C has no label.
Volume Serial Number is 1EDF-1F00

Directory of C:\Users\Eli\Documents\Programming\Java

09/04/2019  09:49 PM    <DIR>          .
09/04/2019  09:49 PM    <DIR>          ..
09/04/2019  09:49 PM               413 Main.class
09/03/2019  04:19 PM               109 Main.java
              2 File(s)            522 bytes
              2 Dir(s)  677,906,145,280 bytes free

C:\Users\Eli\Documents\Programming\Java>java Main.java
Error: Could not find or load main class Main.java

I don't know what seems to be the problem. The class file is there but it says it couldn't find it
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You appear to have done some detective work already, which is good. Please confirm that Main.java contains the proper main() method, with one of the four method headers I mentioned to somebody else yesterday, here. I am sure you will be able to say yes.
Please tell us what happens if you feed the following instructions to the command line (these are [edit]No 2 is a[/edit] Windows®‑specific forms; the Linux/Unix/Mac forms differ slightly):-We often find that users have set a CLASSPATH which is incompatible with using the current directory. That is one of the commonest causes for that particular error.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what version of Java are you using? You can find out by running this command:
> java -version

More recent versions of Java allow you to run programs the way you did: java Main.java

Doing it this way, you don't have to run javac first. With earlier versions of Java (I think prior to Java 11 12) that didn't support this way of running programs, you had to do this:

> javac Main.java
> java Main


Note that when running the program, you don't put the file extension (.java), only the name of the class whose main() method you wanted to execute as the starting point.

Again, starting from Java 11 12, you could combine the two steps and just run java Main.java (Note that you have to give the .java file extension if you run it this way)

Edit: Campbell was right, starting from version 12 you can run a .java file directly without running javac first)
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, post the code you have in Main.java so we can see if there is any problem we can point out there.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Earlier today, I wrote: . . . . . .

I was mistaken; sorry. that should read I thought it is only Java12 that will allow you to write java Main.java, but only under special circumstances including there not being a Main.class file.

Sorry; I missed the error of including the extension ….java in the instruction. Maybe your code will work if you miss out the extension.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I thought it is only Java12 that will allow you to write java Main.java,


but only under special circumstances including there not being a Main.class file.


You're right about from Java 12 but I tried running with a .class file already existing and it ran fine. On my system (Mac OS), the bytecode doesn't get written out to a .class file: the existing .class file that was produced by javac remains intact and unchanged. It looks like the new bytecode just gets generated and kept in memory and fed right to the JVM to be executed. Once the program ends, that's it. If you want a more persistent .class file, it seems you have to run javac just as before.

 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Earlier today, I wrote: . . . . . .

I was mistaken; sorry. that should read I thought it is only Java12 that will allow you to write java Main.java, but only under special circumstances including there not being a Main.class file.

Sorry; I missed the error of including the extension ….java in the instruction. Maybe your code will work if you miss out the extension.



C:\Users\Eli\Documents\Programming\Java>java -cp. Main.java
Unrecognized option: -cp.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

C:\Users\Eli\Documents\Programming\Java>echo %CLASSPATH%
%CLASSPATH%

C:\Users\Eli\Documents\Programming\Java>java -cp. Main
Unrecognized option: -cp.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Did I type anything wrong?
 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, what version of Java are you using? You can find out by running this command:
> java -version



C:\Users\Eli\Documents\Programming\Java>java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

The thing is I have jre 1.8.0_221 installed before I have installed jdk-12.0.2 and I haven't uninstalled jre 1.8.0_221
then when I type java -version in cmd 1.8.0 always shows up and not 12.0.2
 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, post the code you have in Main.java so we can see if there is any problem we can point out there.




It's just a basic code since I just started
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hajile Latni wrote:. . . I have jre 1.8.0_221 installed before I have installed jdk-12.0.2 and I haven't uninstalled jre 1.8.0_221
then when I type java -version in cmd 1.8.0 always shows up and not 12.0.2

Please adjust your PATH so it points to Java12 first, and remove the Java8u221 part from your PATH. Otherwise, when you compile code in Java12 and try to run it as Java8, you will suffer exceptions.

Have you managed to get your code to execute?
 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Hajile Latni wrote:. . . I have jre 1.8.0_221 installed before I have installed jdk-12.0.2 and I haven't uninstalled jre 1.8.0_221
then when I type java -version in cmd 1.8.0 always shows up and not 12.0.2

Please adjust your PATH so it points to Java12 first, and remove the Java8u221 part from your PATH. Otherwise, when you compile code in Java12 and try to run it as Java8, you will suffer exceptions.



So I checked environment varibles and there are three paths that has java in it


which ones should I remove?

Have you managed to get your code to execute?

No
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hajile Latni wrote:So I checked environment varibles and there are three paths that has java in it
which ones should I remove?

Delete 1 & 2. The 3rd one is ok but there's a better way. First, create a brand new environment variable named JAVA_HOME and set its value to C:\Program Files\Java\jdk-12.0.2

Then, modify your path so that this is THE VERY FIRST entry:
%JAVA_HOME%\bin

This helps in two ways: 1) it means that next time you install a new Java release you'll only need to modify JAVA_HOME, and 2) some applications, such as Eclipse depend on JAVA_HOME.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . This helps in two ways:  . . .

Three ways. Moving that PATH component to the beginning of the PATH ensures that you use whichever Java® installation you have as the most recent. At least as long as you update JAVA_HOME whenever you install a new Java® version.
And have you got your problem finding the main method sorted out?
 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Delete 1 & 2. The 3rd one is ok but there's a better way. First, create a brand new environment variable named JAVA_HOME and set its value to C:\Program Files\Java\jdk-12.0.2

Then, modify your path so that this is THE VERY FIRST entry:
%JAVA_HOME%\bin

This helps in two ways: 1) it means that next time you install a new Java release you'll only need to modify JAVA_HOME, and 2) some applications, such as Eclipse depend on JAVA_HOME.



It Worked!!

C:\Users\Eli\Documents\Programming\Java>java -version
java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)

C:\Users\Eli\Documents\Programming\Java>javac Main.java

C:\Users\Eli\Documents\Programming\Java>dir
Volume in drive C has no label.
Volume Serial Number is 1EDF-1F00

Directory of C:\Users\Eli\Documents\Programming\Java

09/06/2019  12:21 PM    <DIR>          .
09/06/2019  12:21 PM    <DIR>          ..
09/06/2019  12:22 PM               413 Main.class
09/05/2019  08:46 PM               109 Main.java
              2 File(s)            522 bytes
              2 Dir(s)  674,127,613,952 bytes free

C:\Users\Eli\Documents\Programming\Java>java Main
Hello World

Thank you so much!!!
 
Hajile Latni
Greenhorn
Posts: 7
Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you everyone!
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!