+ 1

Java (Error: Could not find or load main class ShoppingCart)

Please friends assist me in solving this error. 1) I created a java file named "ShoppingCart.java" and saved it to this folder "C:\javafundamentals\ex03" This is the codes inside the "ShoppingCart.java" file: --------------------------------------------------------------------- package ex03_exercise; public class ShoppingCart { public static void main(String[] args) { System.out.println("Welcome to the Shopping Cart!"); } } --------------------------------------------------------------------- 2) I used windows command prompt to change to the directory that contains the "ShoppingCart.java" file: C:\Users\AKOHPEM>cd C:\javafundamentals\ex03 3) I used windows command prompt to compile it as follow: C:\javafundamentals\ex03>javac ShoppingCart.java Tip: Upon successful compilation of the "ShoppingCart.java", it created "ShoppingCart.class" which is the bytecode that will be used to execute the program. 4) I executed(test) the program as follow: C:\javafundamentals\ex03>java ShoppingCart Error: Could not find or load main class ShoppingCart TIP: Trying to execute(test) the program resulted in the above error. Please I need help in solving the problem, although I have set both "PATH & CLASSPATH" environmental variable as suggested by stackoverflow and oracle yet no avail.

24th Nov 2018, 4:52 PM
Akoh Peter Emeka
Akoh Peter Emeka - avatar
3 Respostas
+ 13
Package name must be the same as the folder name. In your case "package ex03;" is the right name. If you use package names, then you need to run javac and java commands from the root folder. In your case "C:\javafundamentals". For "java" command you also have to prepend package name to the class name, like this: java ex03.ShoppingCart Compilation will be like this: javac ex03\ShoppingCart.java
25th Nov 2018, 7:43 AM
Igor Makarsky
Igor Makarsky - avatar
+ 1
Igor Makarsky I will test it and give you feedback
25th Nov 2018, 8:48 AM
Akoh Peter Emeka
Akoh Peter Emeka - avatar
+ 1
Igor Makarsky thanks very much it worked, you saved me a whole lot more than you think because I have been battling with it for two days.
25th Nov 2018, 2:28 PM
Akoh Peter Emeka
Akoh Peter Emeka - avatar