简体   繁体   中英

Using Eclipse IDE can compile and run the Java program perfectly but when I use 'javac' it is resulting to Error

I am new to Eclipse and java programming, so please be gentle and any help is highly appreciated.

I recently wrote a program using java and Eclipse IDE.

I made the following class:

package Test;
import java.util.*;
import Test.AnotherClass;

public class Program{
  public static void main(String[] args){
    AnotherClass ac = new AnotherClass();
    ac.callingAMethod();
  }
}

This class resides in a file named Program.java and I made another file named AnotherClass.java which is implemented like this:

package Test;
import java.util.*;

public class AnotherClass{
  public void callingAMethod(){
    System.out.println("Hello, World!");
  }
}

Now, if I use Eclipse then the program runs perfectly and even shows the output as "Hello, World!" in the console of Eclipse. But if I use Terminal and javac to compile it gives me the following error:

Program.java:3: error: cannot find symbol import Test.AnotherClass; ^ symbol: class AnotherClass location: package Test Program.java:58: error: cannot find symbol AnotherClass ac = new AnotherClass(); ^ symbol:
class AnotherClass location: class Program Program.java:58: error: cannot find symbol AnotherClass ac = new AnotherClass(); ^ symbol: class AnotherClass location: class Program 3 errors

Another issue is Eclipse creates built in .class files in /bin and if i execute then on the Terminal then it gives me the following error:

Error: Could not find or load main class Program

I can not happen to find the issue, how come the program can compile in Eclipse IDE and show the output as well, whereas when I use 'javac' to compile and then use 'java' to run it is throwing errors.

Any help is appreciated. Thank you.

You should first compile your classes AnotherClass.java and Program.java to create binary files(.class files) like this:

javac Program.java AnotherClass.java 

Then you should go to the directory which the package name "test" exists. Then you should run the below command which also include the classpath while calling the class that contains your main method:

java -classpath . test.Program

When you try to compile Program.java using javac, it's expecting the class file for AnotherClass, as you have referred it in this class, even though eclipse has created this class file when you ran the code javac is not aware of this class file, so solution for first error is

  1. Compile AnotherClass.java using javac first then compile Program.java

  2. The second issue is when you have included a class within package and compile using javac it wouldn't create package structure, to create package structure give below command

    javac -d . AnotherClass.java

    javac -d . Program.java

    java Test.Program

you can replace "." with any directory, I gave "." to create package structure within the same directory. This gives you required output

To use command line to compile, you should specify some parameters other than the class filename in order to compile more than 1 file in one go:

javac -sourcepath src -d bin src\Test\Program.java

It will find and compile all your related classes together with Program.java , ie, AnotherClass.java , located in the sourcepath src , and place all the compiled classes in bin , like what eclipse does.

To execute your program located under bin :

java -classpath bin Test.Program

It will find the compiled classes in bin and execute it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM