1

How can I refer to a Java class in stdlib1.jar when the directory structure is like this? How to write the import statement?

I want to call a method under stdlib1.jar, I have configured it.

screenshot

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101

2 Answers2

0

The classes are in the default package. According to this answer, it is not possible to import classes from the default package. So, they have to be moved to another package or you have to use reflection.

Community
  • 1
  • 1
mm759
  • 1,404
  • 1
  • 9
  • 7
  • do you means file under my project and out of package src can't be use directly unless throught reflection? – Rongwei Chen Dec 22 '16 at 08:32
  • I don't mean the `src`-directory, but the directories within it which represent the Java packages and are also declared at the top of a `java`-file. There must be such a package a class belongs to. If not the default package will be implicitly used. The `src`-directory contains source code. It does not exist in a jar-file that is built from the source code. – mm759 Dec 23 '16 at 07:14
0

You call a method from a class and not from a package.
You don't need to specify the jar when you call a method from a class belonging to it. Which matters is your jar is in the classpath
In your screenshot if the lib makes part of the classpath folders, you can import and use classes from it in your code.

Here the classes of your jar use the default package (no package name) which seems weird for a third-party library. Default package is not recommended since it doesn't allow to naturally reference and use the classes of the archive from the client code.
I am not sure you are using the correct version of the jar.
Look at that :

http://grepcode.com/snapshot/repo1.maven.org/maven2/com.googlecode.princeton-java-introduction/stdlib/1.0.1

This contains classes in the edu.princeton.cs package :

With package, you could declare this :

For example :

You could create a class like that and use BinaryIn like that:

package main;
import edu.princeton.cs.BinaryIn;

public class MyClass(){
  public static void main(String args[]){
    BinaryIn in = new BinaryIn();
  }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thank you.I guess when I use class in the default package , I don't need to import default package .Because there was no wrongs within my IDE(intellij ideal14), it represents that it is a right declaration, but I failed to run it.And thank you for your right code, But I still want to know how can it work in my codition.(next time i will change...) – Rongwei Chen Dec 22 '16 at 08:23
  • Which is exactly your error when you run rhe class and can you post the class ? – davidxxx Dec 22 '16 at 08:29
  • StdIn.class.getPackage().getName(); and console: Exception in thread "main" java.lang.NullPointerException.I can understand StdIn can be found, but it is wired that I can press ctrl and click "Stdln" to get its source code. – Rongwei Chen Dec 22 '16 at 08:36
  • As explained, `StdIn` is located in the default package, which is empty. So when you invoke `StdIn.class.getPackage()` it returns `null` and when you invoke a method on a `null` object, a `NullPointerException` is thrown. – davidxxx Dec 22 '16 at 09:55
  • Thank U.U are right.I got a jar where class all contains in default package, so, wherever I put the foreigher package, I can't invoke it in the right way.But I can invoke it in src>Main.java/test.java>main() file(this way It can invoke method that functions like Scanner.in() method),but I can't invoke it in src>test>test.java>main(). what's the reason? (poor English).I means 2 points:first, default package can't return the properties of classes included.but some special kinds of method of classes included can still work. second, these method don't call success if I call it in some other place. – Rongwei Chen Dec 23 '16 at 06:52
  • second, these method can't be called successfully If this happan in some other catalog/directory/file/place...... – Rongwei Chen Dec 23 '16 at 06:57