如何解决java中不存在的错误

问题描述 投票:3回答:2

我正在尝试编译我的java文件名Test.java。 Test.java调用com.api.APIUser.java类,该文件位于user.jar文件中。我在lib文件夹中添加了user.jar。但是Test.java无法选择APIUser.java。当我使用javac编译Test.java时,我收到错误

"package com.api does not exist".

test.Java

import com.api.APIUser;
 public class Test{
  APIUser ap = new APIUser();
  ap .login();
  public static void main(String[] args){
    //to do
  }

}

APIUser

package com.api
public class APIUser{
  public string login(){
   //to do
   return string;
 }

}

如果有人知道我为什么会收到此错误。请建议我解决。提前致谢。

java
2个回答
0
投票

您的代码中存在多个问题。

  1. APIUser class中,com.api导入没有行终止;
  2. 您的登录方法中存在语法错误。

以下是改进的代码:

import com.api.APIUser;

public class Test {
    // APIUser ap = new APIUser(); // This call should be in the method body,
    // there is no use to keep it at the class level
    // ap.login(); // This call should be in method body
    public static void main(String[] args) {
        // TO DO
        APIUser ap = new APIUser();
        ap.login();
    }
}

APIUser

package com.api; // added termination here

public class APIUser {
    //access specifier should be public
    public string login(){
       //to do
       //return string;//return some value from here, since string is not present this will lead to error
         return "string";
     }
}

还要确保JAR文件存在于类路径中。如果您没有使用任何IDE,则必须使用-cp开关和JAR文件路径,以便可以从那里加载类。

您可以使用下面的代码来了解如何compile your class using classpath from command prompt

javac -cp .;/lib/user.jar; -D com.api.Test.java

0
投票

在com.api包后面加一个分号,如下所示

package com.api;

清理并构建项目并运行,如果有任何问题通知

© www.soinside.com 2019 - 2024. All rights reserved.