0

There seem to be plenty of questions asking how to get the classpath of a given import from inside of java or using an application, but I haven't been able to find a anything on SO about how to find the corresponding path for a given import without the use of any tool or java functionality.

I'd simply like to know how an import is supposed to map according to the contents of $CLASSPATH.

NOTE I'm very new to java; so I apologize if the answer is painfully obvious.

// does this map to "$CLASSPATH/more/libs/for/you/you.jar"
import more.libs.for.you;

The reason why I need to know this is because I'm trying to figure out how to correctly package/install a jna so that I can import it correctly. Currently it's installed under /usr/share/java/jna, but the proper import (according to the docs, if I understand them correctly) is com.sun.jna.* but when I try importing jna I get:

error: package com.sun.jna does not exist

I even tried installing jna under /usr/share/java/com/sun/jna/, but that didn't work either, and I still got the same error.

My $CLASSPATH is /usr/share/java/*:$PWD:$CLASSPATH

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40

1 Answers1

0

In Java, import is entirely syntactic sugar. It does not add things to the classpath or load things into the runtime environment. It simply lets you write code without having to include the full classpath every time you mention the class. And when Java is compiled into Java bytecode, all classes are referenced by their full paths.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I don't see how this answers my question. How do I import a `.jar` if it resides under `/usr/share/java//COOLNAME.jar`? Does java just _know_ that `COOLNAME.jar` is imported like so: `import sys.bunchOfMonkeys.COOLNAME;` as long as it is in the classpath? – Alexej Magura Dec 29 '14 at 20:23
  • you *cannot import a jar*. To use classes within a .jar file, you need to add that .jar file to the classpath or load it with a `URLClassLoader` at runtime – ControlAltDel Dec 29 '14 at 20:31
  • does it have to be added explicitly to the classpath, or would a wildcard work? – Alexej Magura Dec 29 '14 at 20:42
  • This is a question you should be able to find the answer to yourself: http://stackoverflow.com/questions/1237093/using-wildcard-for-classpath – ControlAltDel Dec 29 '14 at 21:05