简体   繁体   English

在Java中导入本地库错误

[英]import local library error in java

I have the In package from Princeton loaded into the same directory as my files, and I compiled it. 我将普林斯顿大学In软件包加载到了与文件相同的目录中,并对其进行了编译。

And, I use the package in my code. 而且,我在代码中使用了该包。 But, when I use import In; 但是,当我使用import In; somehow I still get an error? 不知何故我仍然出现错误?

java:7: error: '.' expected
import In;
       ^

What is the solution to this silly problem? 这个愚蠢的问题有什么解决方案?

The code you linked to has no package . 您链接到的代码没有包

Just delete import In , and somewhere in your code create an instance In myIn = new In(myUrl); 只需删除import In ,然后在代码中的某处创建一个实例In myIn = new In(myUrl); , and you should be good. ,您应该会很好。

Alternatively, modify your copy of "In.java" and make it the same package as you're using for the rest of your code. 或者,修改您的“ In.java”副本,并使其与其余代码所用的程序包相同。

Look at the main() in the code for examples of how to use class "In". 查看代码中的main()以获取有关如何使用类“ In”的示例。

In order to fix this problem, you either need to add a package declaration to In.java that is the same as your package (and then simply omit your import statement), or (my recommendation) you need to add a package declaration to In.java that is different than your package (and move it to the corresponding folder), and then import In by the name of the package that you've given it. 为了解决此问题,您需要在与包相同的In.java中添加一个包声明(然后简单地忽略您的import语句),或者(我的建议)您需要In.java添加一个包声明In.java与您的程序包不同的In.java (并将其移动到相应的文件夹中),然后In您提供的程序包的名称导入。

To make this more concrete: 为了更具体:

  1. Add the following to the top of In.java : 将以下内容添加到In.java的顶部:

     package edu.princeton.cs.introcs.in; 
  2. Then move it to the corresponding "edu/princeton/cs/introcs/in" directory. 然后将其移动到相应的“ edu / princeton / cs / introcs / in”目录中。 (Create it if it doesn't exist). (如果不存在,请创建它)。

  3. Then, in your file, import it by its qualified name: 然后,在文件中按其限定名称导入:

      import edu.princeton.cs.introcs.in.In; 

Note that, in both of the cases above, you'll need to compile In.java along with your code that uses it (or you need to compile In.java, first, and ensure that it is on the classpath when compiling your code that uses it), and at runtime, you need to bundle In.class with your code (eg in the JAR you produce) or similarly guarantee that the byte code for that class (either the .class file or a JAR containing the .class file) are on the class path when executing your compiled code. 请注意,在以上两种情况下,您都需要编译In.java及其使用它的代码(或者,您首先需要编译In.java,并确保在编译代码时它位于类路径中使用它的代码),并且在运行时,您需要将In.class与代码捆绑在一起(例如,在生成的JAR中),或者类似地确保该类的字节码(.class文件或包含.class的JAR)文件)在执行已编译的代码时位于类路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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