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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34196085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:36:02  来源:igfitidea点击:

how to solve package does not exist error in java

java

提问by user3090158

I am trying to compile my java file name Test.java. Test.javacalling a class com.api.APIUser.javawhich is available in a user.jarfile. I have added user.jarin lib folder. But Test.javais unable to pick APIUser.java. While I am compiling Test.javausing javacI am getting error

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

"package com.api does not exist".

Test.java

测试.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;
 }

}

If any one have idea why I am getting this error.please suggest me solution. Thanks in advance.

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

回答by avishka eranga

put a semicolon after the package com.api like as below

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

package com.api;

clean and build the project and run if any issue inform

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

回答by Vivek Singh

You have multiple issues in your code.

您的代码中有多个问题。

  1. You have no line termination present for the com.api import in the APIUser class;
  2. You have a syntax error in your login method.
  1. APIUser class; 中的 com.api 导入没有行终止。
  2. 您的登录方法有语法错误。

Below is the improved code:

下面是改进后的代码:

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

Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cpswitch along with the JAR file path, so that a class can be loaded from there.

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

You can use the code below to understand how to compile your class using classpath from command prompt.

您可以使用下面的代码来了解如何从命令提示符使用类路径编译您的类

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