• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could not find or load main class  RSS feed

 
Timothy Mobley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run java from the command line. It compiles without a problem, but when I run the program with the java command I get:
Error: Could not find or load main class FileName

I've set both the PATH and CLASSPATH environment variables.

Any suggestions appreciated.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

This problem is usually related to package declarations. Please show us your code (and please UseCodeTags ←click that link to learn how)
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please show use whether you can find that file with the dir command.
What did you set CLASSPATH to? Despite the older books telling you to set the CLASSPATH, it usually causes more trouble than it is worth. It causes errors like being unable to find files even when they are there. Please tell us what the CLASSPATH was before you set it. You may have to delete the entire CLASSPATH.

And welcome again
 
Timothy Mobley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working through OCA Study Guide by Boyarsk and Selikoff, and trying to run each little example they give.
The following is one of the example programs that is compiling but not running:



...then in the command line, in the folder where InputImports.java is saved the following runs successfully:

javac InputImports.java

...but when I use the following command to run the program I get the following error:

java InputImports
Error: Could not find or load main class InpurImports

...According to suggestions I found online, I added the following to CLASSPATH:

C:\Program Files\Java\jre1.8.0_25\lib\rt.jar

I've tried it with both with and (originally) without that entry. I also tried deleting the entire CLASSPATH (per Ritchie's suggestion) with the same result.

There are several other examples from the book that won't run, but I should note that some programs DO run just fine, so I'm just trying to figure out why some DON'T (just for my education).

Thanks for any comments!
Tim
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have main() method somewhere?

Are you planning to go for certification? Don't rush those things, it can lead to disappointment. Looking to the code snippet you posted, I don't see there main() method which supposed to be a starting point of such Java application. Now, having said that, I'd have expected you to notice that... Thinking further, you might picked up this book slightly too early - but I could be wrong. Beware, book authors suggests that book isn't meant to be a first book, not sure if it is true in your case.

How much experience do you have?

I'm risking to lead you to go off topic here. Senior moderators hopefully will advice us if we picked up wrong path here.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Mobley wrote:
I also tried deleting the entire CLASSPATH (per Ritchie's suggestion) with the same result.


That is the correct thing to do.
Not sure why the book you have is suggesting adding that CLASSPATH at all.

In any case, though, your actual issue is as Liutauras suggests....you don't have a main() method in that class, and that method is what Java uses to start the program.
 
Timothy Mobley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why the book you have is suggesting adding that CLASSPATH at all.

Just to clarify - the book didn't say anything (yet) about CLASSPATH - that's something I just found when I Googled my problem.

So Ritchie was right... I first deleted all the entries in CLASSPATH (still had problem) but then deleted the CLASSPATH environment variable altogether and that fixed it for some of the programs but not others.

The programs without a main() method run on my home computer, but not on my work computer. My guess (as Vilda alluded to) is that the older release of JRE - which my work computer uses - does not run programs without a main() method, but the newer release does.

Vilda - I'm a sys admin, so I'm very new to programming, but interested in learning Java (and eventually C#).
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I use the following command to run the program I get the following error:

java InputImports
Error: Could not find or load main class InpurImports 

That doesn't make sense.  The classname in the error message is different from the name on the commandline.

Can you copy the actual contents of the command prompt window and paste it here that shows the execution of the javac command and the java command so we can see what happened?

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

I get a different error message with that program:

Running: java.exe -client InputImports

Error: Main method not found in class InputImports, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


Without a class file:

C:\Users\Norm>java ThereIsNoClassFile
Error: Could not find or load main class ThereIsNoClassFile

C:\Users\Norm>
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Mobley wrote:
The programs without a main() method run on my home computer, but not on my work computer. My guess (as Vilda alluded to) is that the older release of JRE - which my work computer uses - does not run programs without a main() method, but the newer release does.


That's not actually the case.
Any Java program you launch from the command line requires a main() method in the launching class as a start point.
Doesn't matter whether it is 1.2 or 1.8.
 
Timothy Mobley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radder - that was a typo, sorry. Same class name, but I think I'm good now.
Tolls - good to know. So I guess the examples in my book without main() weren't meant to be run, but were just code snippets.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is a bona fide class, but it is not a complete application. Classes are parts of applications and they don't usually have main() methods. Only one class per application will have a main method in most cases.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy,

Certification books a meant to teach particular language concepts at a given time. To visualise and replicate concept in a code manner, often is enough to give a small code snippet without surrounding code. Moreover,  there need to be only one main() method within application, to start it, the rest business logic laying down in other classes in most cases, hence they don't run on their own, but you can't rule them out as an incomplete code somehow.
So, when you see main method in a book and lots of code next to it - it is most likely not what you'd see in practice.

 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, nice to see you around
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot, if you're planning to take certification path, we do have OCAJP forum for that. Have a look around, you might find it helpful during your certification journey.
Other than that, you're more welcome to participate in any forum you like - we do have lots of them.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!