How to use stdlib.jar in Intellij Idea?

Answered

Hi i'm learning the Sedgewick's book for Java and i want to use "stdlib.jar" in "Intelij Idea".

I simply go to Project Structure>Modules>Dependencies and add it to the list but when i want to write some code in "Intellij Idea", it seems that it doesn't work and i can't use the codes i want.

I've searched the internet and didn't find anything useful.

Here's a pic:

http://i.stack.imgur.com/FjORt.jpg

Help me if you can.

15 comments
Comment actions Permalink

Make sure the jar contains the class you are using and the jar is added to the dependencies of the module: http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html.

1
Comment actions Permalink

thanks for you fast response. i'm sure that it contains the class and i'm sure the jar is added, that is why i'm so confused about it.

0
Comment actions Permalink

Please share a sample project to reproduce the problem (including all the files and the jars).

1
Comment actions Permalink

ok.wait a moment

0
Comment actions Permalink

thank you for helping:)
here's the jar file : http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar
here's some sample project

package com.company;

public class Main {

public static void main(String[] args) {
// write your code here
StdOut.println("d");

}
}
0
Comment actions Permalink

is anything else needed?

0
Comment actions Permalink

Your jar has all the classes in the default package (root of the jar). Java doesn't support importing classes from the default package, see http://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package for more details.

1
Comment actions Permalink

thank you so much:))))

0
Comment actions Permalink

i still can't solve my problem.i know that i look dumb because i'm new to java programming .would you please help me more?
if we can't use classes in the default package, what are they used for?

isn't there an easy way to use them?

0
Comment actions Permalink

is there an easy way to use the classes i need?

0
Comment actions Permalink

Here is one of the ways to fix it: ftp://ftp.intellij.net/pub/.idea/StdLibSample.zip.

I've unpacked the sources from the jar and Refactor | Moved them into stdlib package so that they can be imported.

You can then package them back into the jar inside stdlib package folder and use the jar library instead of having the sources for the library as in the sample project above.

0
Comment actions Permalink

i ca't run your sample code in intellij idea. the run button is off.sorry for being dumb :(

0
Comment actions Permalink

Right click on Main and run it from the context menu:

Make sure correct JDK is selected for the project:

0
Comment actions Permalink

The website also have a package version of the library.  Use this instead.

http://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar

Then you can use import statement:

import edu.princeton.cs.introcs.StdOut;
0

Please sign in to leave a comment.