• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

javac - Could not find or load main class

 
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I have the following code:



I saved this file in the name "file.java".

In order to run it, I opened my terminal in the containing folder of this file and typed:
> javac file.java
> java ChainOfResponsibilityEverydayDemo

I got the error:

Error: Could not find or load main class com.pluralsight.chain.ChainOfResponsibilityEverydayDemo



Do you know what I'm doing wrong?
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file name must be the same as the public class that's declared in it.
 
Josh Abraham
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the file name to 'ChainOfResponsibilityDemo', and I still get the same error.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Josh Abraham wrote:I changed the file name to 'ChainOfResponsibilityDemo', and I still get the same error.



That's because the names have to match EXACTLY, not almost exactly.  A public class Foo has to be saved in a file named Foo.java. Pay attention to the details.
 
Josh Abraham
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, it's:

$ javac ChainOfResponsibilityEverydayDemo.java
$ java ChainOfResponsibilityEverydayDemo
Error: Could not find or load main class ChainOfResponsibilityEverydayDemo
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably because you have it in a specific package, com.pluralsight.chain.  Is your Java file in a directory that reflects that?  The path to your file should be something like  ..\com\pluralsight\chain\ChainOfResponsibilityEverydayDemo.java. Then you'd run the program from the directory right above com, like this:

java com.pluralsight.chain.ChainOfResponsibilityEverydayDemo
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is the error coming from the compiler (javac) or the JVM (java)?  And is that it? That is the only error message you are getting?

Henry
 
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Renamed mine to Test for typing ease.


 
Josh Abraham
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I removed the line "package...", it worked properly.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


That doesn't always work. I created ../com/javaranch/beginner/Foo.java and still get "Error: Could not find or load main class Foo" with java -cp .:com/javaranch/beginner Foo
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Josh Abraham wrote:When I removed the line "package...", it worked properly.


That's fine for toy and demo programs but remember that you normally would have a package declaration for real programs.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Carey Brown wrote:


That doesn't always work. I created ../com/javaranch/beginner/Foo.java and still get "Error: Could not find or load main class Foo" with java -cp .:com/javaranch/beginner Foo


Clarification needed. Which one of these did you use

I used the second one
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
That doesn't always work. I created ../com/javaranch/beginner/Foo.java and still get "Error: Could not find or load main class Foo" with java -cp .:com/javaranch/beginner Foo



The classpath is for the root directory of the class files. The name of the class, with fully qualified package name, is com.pluralsight.chain.Test.

So assuming that the Test.class file is in c:\temp\hwong\com\pluralsight\chain directory, then the command should be...

> java -cp \temp\hwong  com.pluralsight.chain.Test

Henry
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Josh Abraham wrote:When I removed the line "package...", it worked properly.


That's fine for toy and demo programs but remember that you normally would have a package declaration for real programs.



Correct -- "worked  properly" and "no complaints" are not the same thing. Basically, the JVM is complaining due to incorrect use of a feature (packages), and the solution proposed is to not use the feature. That isn't a really good way to learn the feature either.

Henry
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
Clarification needed.


 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Junilu Lacar wrote:
That doesn't always work. I created ../com/javaranch/beginner/Foo.java and still get "Error: Could not find or load main class Foo" with java -cp .:com/javaranch/beginner Foo



The classpath is for the root directory of the class files. The name of the class, with fully qualified package name, is com.pluralsight.chain.Test.

So assuming that the Test.class file is in c:\temp\hwong\com\pluralsight\chain directory, then the command should be...

> java -cp \temp\hwong  com.pluralsight.chain.Test

Henry


Not disagreeing with you here but it isn't behaving that way for me. Undoubtedly I'm doing something wrong.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this work for you? I think it should.

We should probably put all this stuff into the Beginner FAQ - it's a very common problem that pops up every few days.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Does this work for you? I think it should.


 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you confirm that your Test class is in the correct package?

Henry
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Can you confirm that your Test class is in the correct package?

Henry


<Big splotch of egg on face.>

At the beginning of this thread I had commented out package and forgot to uncomment. Humble apologies.

Thanks Henry
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic