• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Error on command prompt when running a class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am getting the below error on the command prompt when I am trying to run a class with this command: java packageb.ClassB

Error: Could not find or load main class packageb.ClassB

I have followed all the instructions from the OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808.pdf course from the Compiling Code with Packages in chapter 1, so I do not understand what I am doing wrong. Please assist

Thanks
 
Marshal
Posts: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welome to the Ranch

Please don't think about running a class; you are running a method; in this case the method you are trying to run is the main() method.
Please tell us how you compiled the code, and what the contents of your directory are. You should have a directory visible from your current location called packageb and that should contain a file named ClassB.class
Also: did you set a CLASSPATH?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It would be best if you copy-pasted all the relevant text from the console so we can see exactly what you tried. Also, use dir or las to list the directories you're working in and what files are in them. A class packageb.ClassB should be in a file name ClassB.java that is in a folder named pakageb. You need to use the javac command to compile it before you can run it with the java command.
 
Kerri Efune
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled the code using this command: javac packagea/ClassA.java packageb/ClassB.java

Below is the content of the java file

package packageb;
import packagea.ClassA;
public class ClassB {
public static void main(String[] args) {
ClassA a;
System.out.println("Got it");
}
}


I have also attached a screenshot of the command prompt as well as the content of the folders
 
Kerri Efune
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why my attachment didn't attach, but the below is from the command prompt

C:\JavaPractice>set path=C:\Program Files\Java\java-1.8.0-openjdk\bin

C:\JavaPractice>javac packagea/ClassA.java packageb/ClassB.java

C:\JavaPractice>java packageb.ClassB
Error: Could not find or load main class packageb.ClassB

C:\JavaPractice>

The below are my folder paths and they have both the (ClassA.java and ClassA.class)and (ClassB.java and ClassB.class) files respectively in them

C:\JavaPractice\packagea
C:\JavaPractice\packageb
 
Campbell Ritchie
Marshal
Posts: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kerri Efune wrote:. . . I have also attached a screenshot . . .

You don't appear to have added it, but it is better to avoid screenshots; they are often difficult to read.
Try compiling classA on its own first.
Don't set the PATH to contain one directory only. Write set path=C:\Program Files\Java\java-1.8.0-openjdk\bin;%PATH%
Otherwise I can't see anything wrong with what you posted.
There remains the possibility that you have set a CLASSPATH. Try setting a local CLASSPATH. Try java -cp . packageb.ClassB

[edit]Correct minor spelling error producing ਏ character.
 
Kerri Efune
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah thanks a lot, setting that classpath resolved it
 
Campbell Ritchie
Marshal
Posts: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure

I think the real problem may be that you have set a system or user CLASSPATH. Please show us what happens when you write echo %CLASSPATH% and you will probably have to go to your environment variables and delete the CLASSPATH.
 
Kerri Efune
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I get the below path when i wrote it

C:\JavaPractice>echo %CLASSPATH%
C:\Program Files (x86)\Altova\xmlspy\XMLSpyInterface.jar

If I delete this path, would it interfere with the software I am using above
 
Campbell Ritchie
Marshal
Posts: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kerri Efune wrote:. . . C:\JavaPractice>echo %CLASSPATH% . . . If I delete this path . . .

Maybe the nomenclature is a bit confusing: the CLASSPATH comprises any number of paths to resources.

That doesn't count as part of the PATH environment variable. What that is doing, as part of the CLASSPATH, is saying to any code using it,

You will always need the SpyInterface resource, and nothing else.

Please delete the whole of the system or user CLASSPATH and use -cp or set CLASSPATH=... when you actually need to change it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic