2

I need to import a Java class in my project. The class I need to import is in a JAR file with absolute path ~/Documents/mylib/stdlib.jar

Now in my source file (Test.java) I am using

import stdlib.*;

and I am compiling with javac -classpath ~/Documents/mylib/stdlib.jar Test.java

And it is showing the error that stdlib does not exist.

Is my import statement correct or is there anything wrong with classpath? Shouldn't this import statement import all the classes present in the JAR file?

I am not using any IDE and my OS is Linux.

informatik01
  • 16,038
  • 10
  • 74
  • 104
deep19
  • 81
  • 1
  • 1
  • 11

4 Answers4

3

The import statement imports classes from the jar file, not the jar file itself.

An import statement of the form:

  import stdlib.*;

will import all the classes in the package stdlib.

Oracle provides this tutorial on import.

EDIT: It looks like you're using the stdlib.jar for An introduction to programming in Java. The classes in this jar file have no packages. You don't need to import classes in the default package, since your class is also in the default package.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • I need to import a class from the jar file.What should I do? – deep19 Dec 07 '11 at 15:53
  • Say you want to import a class Foo that's in package com.bar -- in any jar file on the classpath. Then you could use either import com.bar.* OR import com.bar.Foo. The first imports all the public classes in the package; the latter, just one. If you're using an IDE, it may support adding the import for you. – Andy Thomas Dec 07 '11 at 15:57
  • Are you certain that the package stdlib exists inside the jar file? Can you provide a link to either the API docs or the library itself? – Andy Thomas Dec 07 '11 at 16:04
  • If there are no packages, then to import a class Foo just use "import Foo". – Andy Thomas Dec 07 '11 at 16:13
  • Well I tried just "import StdOut" (StdOut is the class I need to import which is in stdlib.jar which I set in classpath) but then also it is showing error – deep19 Dec 07 '11 at 16:22
  • What do you see when you enter "javac -version"? – Andy Thomas Dec 07 '11 at 16:31
  • I tried import StdOut and it says : Test.java:1: '.' expected import StdOut; ^ Test.java:1: ';' expected import StdOut; ^ 2 errors – deep19 Dec 07 '11 at 16:42
  • Did you include a semicolon at the end: "import StdOut;" ? If you did, then can you show us your source code? – Andy Thomas Dec 07 '11 at 16:45
  • import StdOut; public class Test{ public static void main(String args[]){ StdOut.println("Helloz"); } } – deep19 Dec 07 '11 at 16:47
  • Okay, just take out the import. That should fix the problem. You don't need to import classes from the default package -- since your class is itself in the default package. It's unusual for classes not to be in a package, so this is less familiar for most of us. – Andy Thomas Dec 07 '11 at 16:56
  • @AndyThomas-Cramer In fact you *cannot* import classes from the default package. – Dave Newton Dec 07 '11 at 17:02
  • Even after removing import I am getting error : Test.java:6: cannot find symbol symbol : variable StdOut location: class Test StdOut.println("Helloz"); ^ 1 error – deep19 Dec 07 '11 at 17:07
  • Is your class in a named package? If so, one fix is to move your class to the default package. Java no longer supports importing classes from the default package, since 1.4 or so. – Andy Thomas Dec 07 '11 at 17:10
  • By default package you mean current directory?? – deep19 Dec 07 '11 at 17:15
  • Above your source code for "class Test", do you have a "package" statement? – Andy Thomas Dec 07 '11 at 17:18
  • I compiled and ran your source code (minus the import) without error, using the library I guessed above. It worked. I think you're going to have to double-check everything in your environment. – Andy Thomas Dec 07 '11 at 17:29
  • I am able to compile the code without any error after removing the import.BUt on running the code it says a Class Not found exception for StdOut.I copied the StdOut.java in my current directory and I am getting the result but I want to dont want to copy the files everytime.So is my classpath settings correct : javac -classpath Test.java – deep19 Dec 07 '11 at 17:42
0

It will import all classes immediately under the "stdlib" package, but nothing in sub-packages. You need to import the package(s) and classes; we can see inside the jar to know if your package name assumption is correct.

The class path itself is fine.


Updated with information from comments.

You say that you're trying to import a class in the default package. It is a syntax error to import a type from the default package. See the JLS Section 7.5 for details.

It is a compile time error to import a type from the unnamed package.

If you must access this class you'll need to use reflection.

It would be much easier to put it in a named package, though.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • The file I need to import is in the stdlib.jar file , no sub-packages – deep19 Dec 07 '11 at 15:58
  • @user966518 You don't import a file, you import a package's classes, or a specific classes. What are the fully-qualified names of the classes in the jar? – Dave Newton Dec 07 '11 at 16:08
  • StdOut.java is the class I want to import – deep19 Dec 07 '11 at 16:14
  • @user966518 I understand it's in the jar. What's the fully-qualified name (including the package) of the class? If it's in the default package, just import the class itself. – Dave Newton Dec 07 '11 at 16:15
  • Well I dont know what "fully qualified name" means (I am noob).Its just that on extracting the jar file I get the class I need to import.The name of the jar is "stdlib.jar" and the file I need to import is "StdOut.java".So I need to know what import statement should I use "import stdlib.*;" or just "import StdOut".Also IS my classpath correct? – deep19 Dec 07 '11 at 16:20
  • The classpath is fine. *We cannot see inside your jar.* We do not know what packages it contains. Do a `jar tf stdlib.jar` to see what the packages/classes are. We *know* you need to know the `import` statement to use, and we're trying to help with the limited information you're providing. – Dave Newton Dec 07 '11 at 16:27
  • Better yet, do jar tf ~/Documents/mylib/stdlib.jar – Andy Thomas Dec 07 '11 at 16:32
  • this gives output: StdOut.java StdOut.class and other .java files.There are no other packages just plain .java in the jar. – deep19 Dec 07 '11 at 16:39
  • I tried that it is still showing error : Test.java:1: '.' expected import StdOut; ^ Test.java:1: ';' expected import StdOut; ^ 2 errors – deep19 Dec 07 '11 at 16:44
  • import StdOut; public class Test{ public static void main(String args[]){ StdOut.println("Helloz"); } } – deep19 Dec 07 '11 at 16:50
  • Its saying that instead of semicolon after StdOut I should have a "." – deep19 Dec 07 '11 at 16:53
  • @user966518 If your class is also in the default package, then you don't need to import the class at all. – Dave Newton Dec 07 '11 at 16:55
  • But if I remove the import statement its not recognising StdOut in StdOut.println() statement. ERROR : Test.java:6: cannot find symbol symbol : variable StdOut location: class Test StdOut.println("Helloz"); ^ 1 error – deep19 Dec 07 '11 at 17:01
  • Is your class in a named package? – Andy Thomas Dec 07 '11 at 17:05
  • Well I dont understand the concept of package very well.The class is in the "top-level" of the jar file so I guess it isnt in any package as such. – deep19 Dec 07 '11 at 17:13
  • @user966518 He's asking about the class you're using StdOut in. – Dave Newton Dec 07 '11 at 17:14
  • Right - in your own source code for class Test, do you see a "package" statement near the top? – Andy Thomas Dec 07 '11 at 17:16
  • Well,as I said I dont understand the concept of package very well so I dont know what it means.Its just a file Test.java in a directory.The source code of which I had written above. – deep19 Dec 07 '11 at 17:19
0

Use $HOME instead of ~.

$HOME is expanded, ~ is not.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

Check whether StdOut.java has a declaration package stdout;.

Or is it just situated on the top-level folder in stdout.jar?

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
korifey
  • 3,379
  • 17
  • 17
  • On extracting stdlib.jar (the jar file I set in classpath) I get StdOut.java (the class I need to import) – deep19 Dec 07 '11 at 16:27