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.
$ javac ChainOfResponsibilityEverydayDemo.java
$ java ChainOfResponsibilityEverydayDemo
Error: Could not find or load main class ChainOfResponsibilityEverydayDemo
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:
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
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
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...
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.
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.