0

我在编译这些 java 文件时遇到问题。我有一个类 MeterMovementService.java 和一个接口 MeterMovementServiceMBean.java 。该类正在实现这个接口。我首先编译了接口。类和接口都位于同一个包中。但是当我尝试编译该类时,它会给出错误:

MeterMovementService.java:2: error: cannot find symbol
public class MeterMovementService  implements MeterMovementServiceMBean
                                              ^
  symbol: class MeterMovementServiceMBean
1 error

这是 MeterMovementService.java 类的代码

我的类没有依赖。它只是实现了接口。

  public class MeterMovementService  implements MeterMovementServiceMBean {
   private String message = "Sorry no message today";
   public String getMessage(){
      return message;
   }
   public void setMessage(String message){
      this.message = message;
   }
   public void printMessage(){
      System.out.println(message);
   }
   public void start() throws Exception{
       System.out.println(">>>>Starting with message=" + message);
   }
   public void stop() throws Exception{
       System.out.println(">>>>Stopping with message=" + message);
   }
}
4

2 回答 2

1

You need to ensure that your class files are placed under the package folder. For e.g. if your interface is defined under package - pkg1, your class file should be under pkg1 subfolder.

Your directory structure should be like this:

srcfolder -+
           +- pkg1 -+- MeterMovementServiceMBean.class
           |        +- MeterMovementService.class
           |
           +- MeterMovementService.java
           +- MeterMovementServiceMBean.java

Either you have to move the class files manually, or even better, you can compile your .java files using the below command, to let the compiler handle it all for you:

javac -d . MeterMovementServiceMBean.java
于 2013-06-27T20:02:52.623 回答
1

javac -d . *.java编译当前目录下的所有java文件,并根据包结构对编译后的classfile进行打包。

于 2013-06-28T09:46:06.480 回答