0

I was studying the Java package system. I read something from foruns and from the Oracle docs.

My idea was create two classes. Put them in different packages, and use the import keyword to make all run.

The Class01 is like this:

package study.lab03;

public class Class01{
    public void execute(){
        System.out.println("test ok");
    }
}

The Class02 is like this:

package study;

import study.lab03.*;

public class Class02{
    public static void main(String[] args){
        Class01 cl01 = new Class01();
        cl01.execute();
    }
}

My folder structure is like this: C:\projects\study\lab03

I have added 'C:\projects' to the final of the CLASSPATH variable.

To compile Class01 I did: C:\projects\study\lab03> javac Class01.java Compilation was good and the .class file was created.

To compile Class02 I did: C:\projects\study>javac Class02.java Compilation was good again and the .class was created.

To run the code I'm trying:

C:\projects\study>java Class02 Error: Impossible to find or load the main class

C:\projects\study>java -classpath projects Class02 Error: Impossible to find or load the main class

C:\projects\study>java -classpath projects study.Class02 Error: Impossible to find or load the main class

I can't understand what I'm doing wrong.

heihel
  • 1
  • 2
  • Have you checked your CLASSPATH environment variable? – Thomas Nov 27 '19 at 22:21
  • 5
    If you are in the `study` subfolder then you are actually **in** the package system, not a place where you ever want to be. Go one folder back and add `c:\projects` (absolute path) or `.` (relative path) in the class path. – Maarten Bodewes Nov 27 '19 at 22:23
  • 1
    Note that usually we put source files in "\src" using the package names as subfolders, so you would have `c:\projects\study\src\study\lab03`. Otherwise all the packages would all be in the same project. – Maarten Bodewes Nov 27 '19 at 22:27

1 Answers1

0

You need to spec the FQN (fully qualified name, ie. packages + class name) of the class who's main() you want to run. This then looks like so in your case: C:\projects>java study.Class02

Note, if you are in C:\projects then you don't need to specify the -classpath because that is by default . If you are elsewhere then you need to spec it.

elonderin
  • 509
  • 4
  • 12