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");
    }
}
0

1 Answer 1

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/*
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I'm not sure I still like the "run single .java files" feature.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.