The Stack Overflow podcast is back! Listen to an interview with our new CEO.
1 of 2

Running Java classes in a package --> Error: Could not find or load main class

I'm learning how to compile and run java classes in terminal using the commands javac and java. I have no problem compiling and running an individual class, however, when I put that class into a package and try to run it, I get "Error: Could not find or load main class (class name)".

Currently, my directory structure is: ~/Desktop/OCA_Practice/cert the name of the file I'm running is called and I've placed it in a package called cert. Here is my very basic class:

package cert; public class Test { public static void main(String args[]) { System.out.println("This is a test."); } }

When I run the command javac in the cert directory, the class compiles successfully, however, when I try to run java Test after compiling, that's when I get the error. I've tried running my folder structure in addition to the class when I try to run the program, as in java Desktop.OCA_Practice.cert.Test and that didn't work. I've changed my package declaration inside my class to Desktop.OCA_Practice_Probs.cert and that didn't work either.

I've tried a large variety of different approaches with no success. Have I written the package name wrong in my class file? I ran the command: java -version and it shows that I'm running java version "1.8.0_25" so I'm pretty sure my class path is set correctly.

Thanks in advance for any responses.