• Post Reply Bookmark Topic Watch Topic
  • New Topic

javac - Could not find or load main class  RSS feed

 
Josh Abraham
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
  • 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?
 
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
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
  • Quote
  • Report post to moderator
I changed the file name to 'ChainOfResponsibilityDemo', and I still get the same error.
 
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
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
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
When I removed the line "package...", it worked properly.
 
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
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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Clarification needed.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Does this work for you? I think it should.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you confirm that your Test class is in the correct package?

Henry
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!