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.

0
18 comments

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
Avatar
Permanently deleted user

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

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

1
Avatar
Permanently deleted user

ok.wait a moment

0
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

is anything else needed?

0

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
Avatar
Permanently deleted user

thank you so much:))))

0
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

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

0

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
Avatar
Permanently deleted user

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

0

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

Make sure correct JDK is selected for the project:

0
Avatar
Permanently deleted user

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;
2

Thank you @.... This solves my issues.

0
Avatar
Permanently deleted user

I have the same issue. Could you please help out? How to configure this stdlib package? 

Thanks!

0

@... Please see: https://www.jetbrains.com/help/idea/working-with-module-dependencies.html#add-a-new-dependency

1. File > Project Structure... or press Ctrl + Alt + Shift + S.
2. Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
3. Select the jar file and click on OK, then click on another OK button to confirm.
4. You can view the jar file in the "External Libraries" folder.

1

Please sign in to leave a comment.