0

I am very new to Java programming. Since my original java class contains too many methods, I want to move some of them to another class, so I created a new class MergerHTML.

Before having the second class file, I was using command "javac -cp "./lib/*:lib/*" src/AAA.java" to compile and using command "java -classpath "lib/*:lib/*" src/AAA.java data/*" to run the program. I am already confused here. If I do not put ".java" in the run command, the message:

"Error: Could not find or load main class src.AAA
 Caused by: java.lang.ClassNotFoundException: src.AAA"

would appear. Why is this happening?

After adding the second class file. I used the command "javac -cp "./lib/*:lib/*" src/AAA.java src/MergerHTML.java" to compile the program, and no error found. However, when I used command "java -classpath "lib/*:lib/*" src/AAA.java data/*", the following is the resulting error:

src/AAA.java:441: error: cannot find symbol
    MergerHTML mHTML = new MergerHTML();
    ^
    symbol:   class MergerHTML
    location: class AAA

My main class file look like below:

public class AAA {
   ....
   public static void main(final String[] args) throws Exception {

       MergerHTML mHTML = new MergerHTML();
       mHTML.print();

   }
}

and helper class file look like below:

public class MergerHTML{
    public void print(){
        System.out.println("hiiiiii");
    }
}
Jin Lee
  • 3,194
  • 12
  • 46
  • 86

1 Answers1

0

Instead of

java -classpath "lib/*:lib/*" src/AAA.java data/*

You need to specify the compiled class to execute (not the source it came from), you don't need lib/* twice in your classpath, but you do need the current folder (actually, the folder containing your compiled classes, which is the current folder in this case). Like (change : to ; on Windows),

java -classpath "lib/*:." AAA data/*

Note: The more conventional place to put the classes would be a bin folder (you can name it whatever you like, but it's more convenient to package when the classes are all collected in a "clean" tree). So, something like

mkdir bin
javac -cp "lib/*" -d bin src/AAA.java src/MergerHTML.java
java -classpath "lib/*:bin" AAA data/*

Finally, if you're on Windows, the last command above should be

java -classpath "lib/*;bin" AAA data/*
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you for such a quick and thorough reply! It really helps me a lot! I just realized that when running the java program, I should be in the directory of `AAA`. Otherwise, I have to add ".java" end the end of the class name. Moreover, my AAA.class is located at the same directory as MergerHTML.java which is at ./src. I think this directory should be the same as the bin folder you mentioned. – cao scarlett Aug 28 '19 at 03:02
  • I'm not sure I still like the "run single .java files" feature. – Johannes Kuhn Aug 28 '19 at 03:38