Forums Register Login

javac - Could not find or load main class

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
The file name must be the same as the public class that's declared in it.
+Pie Number of slices to send: Send
I changed the file name to 'ChainOfResponsibilityDemo', and I still get the same error.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Now, it's:

$ javac ChainOfResponsibilityEverydayDemo.java
$ java ChainOfResponsibilityEverydayDemo
Error: Could not find or load main class ChainOfResponsibilityEverydayDemo
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
Renamed mine to Test for typing ease.


+Pie Number of slices to send: Send
When I removed the line "package...", it worked properly.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

Carey Brown wrote:
Clarification needed.


+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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


+Pie Number of slices to send: Send
Can you confirm that your Test class is in the correct package?

Henry
+Pie Number of slices to send: Send
 

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
Grow a forest with seedballs and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 7258 times.
Similar Threads
Java logging API
Changing log level on Logger
problem with java logging
Logging (java.util.logging)
Java Logging API
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:51:49.