How to use a external jar in a Java application

I'm trying to set up IntelliJ IDEA (13 and 12, on Mac) to use a Java class included a jar file for my Java application.  

The two jars are downloaded from http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar and http://algs4.cs.princeton.edu/code/algs4.jar .

I've set the jar as a module dependency by the following procedure

* Start from the "project window";
* Select my main Java module;
* Use menu File | Project Structure;
* In Project Settings, select Modules, and select my Java application as interested module in the middle window;
* On the window to the right, select "Dependencies" tab, and press "+" and select "Jars or directories" and navigate to the jar file, add select it so the custom jar file is added as a new entry in the window, check the Export checkbox and set Scope pulldown set as "Compile";
* Press OK.

However, after setting this, in a Java class file in my Java application, if I use a class defined in the custom jar file, it still prompts me "Cannot resolve symbol 'SomeClass'".

Any idea what I'm doing wrong and how to fix this?

my settings:

Screenshot 2014-03-27 00.09.10.png

Screenshot 2014-03-27 00.08.10.png


The Libraries and Global libraries are both empty currently.  I did try to set the two jars there (separately) and add them as dependencies for the Java app, and it did NOT work, so I deleted them from Libraries and Global libraries. The above setting is using add module dependency as files directly (without first registering them as libraries), which still does not work.

Please help!  I'm desperate.  Have spent many many hours on this.

6 comments
Comment actions Permalink

Hello.

As far as I can see in your story, you do everything correctly, and it should work. Can you attach a zipped sample project that does not?

Regards,
Alexander.

0
Comment actions Permalink

Here is the zipped file of the whole IntelliJ project 'percolation': https://www.dropbox.com/s/dmkzckyoy7koky5/percolation.zip .  The QuickUnionUF class is an example that a class defined in the library but can't be find by IntelliJ.

I posted the question here and in Stackoverflow. Unfortunately so far neither get solution. As i really would like to get the work done, I'd have to try Eclipse, even though I really would like to work in IntelliJ.

0
Comment actions Permalink

Thank you. Am I correct that the QuickUnionUF reference is red?  Then there are two problems, one is in your project, another is ours.

In your attached project the error is correct. QuickUnionUF is placed in the default package (no package statement), and modern versions of java (at least 7 and 8) forbid to refer classes in default package, unless your own class is in default package too. That's the problem in the project.

However, I've put referring class into default package, and got the same error. And it looks like a bug. I've created http://youtrack.jetbrains.com/issue/IDEA-123125

Regards,
Alexander.

0
Comment actions Permalink

P.S. In general, classes in default packages in java are typically just samples. All real projects have classes in packages. That's the standard in java world.

0
Comment actions Permalink

Thanks for the diagnosis.  What's a workaround that may work currently?  It's a headache to maintain two IDE envrionment.  I do have other projects done in IntelliJ that does not have external-jar dependencies. In other words, is it possible to adapt the instruction http://algs4.cs.princeton.edu/mac/ to IntelliJ IDEA 12 or 13.1.1?

Quoting the instruction, how the environment is set up:

What does the installer do? In short, it downloads, installs, and configures our textbook libraries, Checkstyle, Findbugs, and DrJava. Here is a more detailed list:

  1. Checks that Java is installed.

  2. Downloads the textbook libraries from stdlib.jar. and algs4.jar.

  3. Downloads the wrapper scripts javac-algs4 and java-algs4.

  4. Downloads and installs Checkstyle 5.5 from checkstyle.zip. Downloads our checkstyle configuration file checkstyle.xml and wrapper script checkstyle-algs4.

  5. Downloads and installs Findbugs 2.0.1 from findbugs.zip. Downloads our findbugs configuration file findbugs.xml and wrapper script findbugs-algs4.

  6. Downloads and installs the latest stable version of DrJava, from drjava-osx.tar.gz. Creates a shortcut to DrJava on the desktop. Downloads and installs the DrJava configuration file from drjava-config.txt to /Users/username/.drjava. Note that this will overwrite any existing .drjava configuration file.

  7. Tests that the installation succeeded by compiling and executing TestAlgs4.java.


I essentially only care that the classes in these two jars can be used in IntelliJ.

0
Comment actions Permalink

If you have control over algs4.jar and stdlib.jar, you could repack them, moving all classes into some package. No more certain ideas.

Regards,
Alexander.

0

Please sign in to leave a comment.