简体   繁体   中英

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.

And, I use the package in my code. But, when I use 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); , 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.

Look at the main() in the code for examples of how to use class "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.

To make this more concrete:

  1. Add the following to the top of In.java :

     package edu.princeton.cs.introcs.in; 
  2. Then move it to the corresponding "edu/princeton/cs/introcs/in" directory. (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.

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