Reputation: 601
I've downloaded two jars. I want to import some of their contained classes. How do I do this?
It's for my algorithms class. I've tried following the instructions on the following site to no avail.
http://algs4.cs.princeton.edu/code/
There's an installer for OSX (I'm running Mountain Lion) which allegedly adds the jars to your classpath. Unfortunately it also installs Dr. Java. I'd rather just use Sublime and Terminal. I assumed it would be easy enough just...
import java.stdlib;
in my Percolation.java file, but javac-ing that program yields a "package stdlib does not exist", as does
import stdlib;
I've added the location of stdlib.jar and algs4.jar to my Terminal CLASSPATH manually via:
export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar/algs4.jar:/Users/Michael/path/to/jar/algs4.jar
export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar/stdlib.jar:/Users/Michael/path/to/jar/stdlib.jar
I've also attempted
javac -cp $CLASSPATH:/Users/Michael/path/to/jar/algs4.jar:/Users/Michael/path/to/jar/stdlib.jar Percolation.java
But I still get a
javac Percolation.java
Percolation.java:1: cannot find symbol
symbol : class stdlib
location: package java
import java.stdlib;
^
Percolation.java:2: package java.algs4 does not exist
import java.algs4.WeightedQuickUnionUF;
^
What's going on here?
Also is there a way to permanently add those values to my CLASSPATH in OS X mountain lion. I have to perform that command with every new Terminal.
Upvotes: 13
Views: 17196
Reputation: 1
I had this same problem. Renfei Wang's solution worked for me (I don't have enough points yet to comment directly on his response).
In Sublime, navigate to Preferences: Browse Packages>JavaC.sublime_build
.
Here's what mine looks like:
{
"cmd": ["javac", "-cp", "/Users/jason/Documents/lib/*:./","$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
/Users/jason/Documents/lib/*:/
lets Sublime know the location of the directory that holds my packages, so that now when I build, it loads those packages first.
Upvotes: 0
Reputation: 1
Open your Sublime
Choose the Tools->Build System->New Build System
Add below code to the new file
This can tell the sublime to run the commands
{
"cmd": ["javac -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file\" && java -cp java -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file_base_name\""],
"shell":true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
Hope this can help those who are following the Algorithm Course from Princeton University
Upvotes: 0
Reputation: 52366
If you are getting the "cannot be resolved to a type" error, and have tried adding the stdlib.jar or algs4.jar files, here is the solution:
The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use the libraries with a named package, you can use these package versions:
stdlib-package.jar and algs4-package.jar.
You can download these files here: http://algs4.cs.princeton.edu/code/
Then you can automatically add the import: import edu.princeton.cs.algs4.ClassName
Upvotes: 0
Reputation: 376
My solution was to add 2 new build systems to my Sublime text editor: one to compile and the other to execute. Use Tools->Build_system->New_build_system... from main menu with these two code snippets:
for compilation (I've named the file "algs-compile.sublime_build"):
{
"cmd": ["javac", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
for running ("algs-run.sublime_build")
{
"cmd": ["java", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file_base_name"]
}
Don't forget to replace paths to jar-files here with the correct ones from your system. I understand that this question is rather old but I hope this will help somebody.
Upvotes: 0
Reputation: 51
just name the package to default then it will work fine . also after that you dont need to import anyting just run some code provided in
Fundamentsls
chapter like
average
.
Upvotes: 0
Reputation: 80
I wasted a lot of time with importing the class, tried the CL option of "javac -cp .;stdlib.jar mad.java" etc but used to get the same error you mentioned.
I then commented out the import altogether and made sure the DrJava's preferences had the 2 classpaths added + the %CLASSPATH% variable to have the right value. Is simply works now.
Good luck!
Upvotes: 0
Reputation: 113
If you're using Eclipse (as I do), select the current project, then you open the project properties from the menus. On the left you select "Java Build path", and then you select the tab libraries. Now you click the button "Add external Jars" and you point to your jar files, and you're done.
Good luck.
Upvotes: 1
Reputation: 306
I faced the same problem during work on this course, but for windows. I'll leave this comment here in case it will help somebody.
If you use DrJava you don't need any import
statements in code. If you followed installation steps described in course, everything is configured for you.
But here can be a problem - it puts jar
files in your current user directory, path to which can contain inappropriate symbols(russian letters in my case). You need to check it in Edit -> Preferences menu. You can see there algs4.jar
and stdlib.jar
paths. Make sure that this path are correct and point to real existing files.
I just moved all necessary files to another dir and changed paths in this menu. It solved this problem for me.
Upvotes: 0
Reputation: 91
It is outdated to answer this question, but maybe it will be useful for future participants of Princeton Algorithms course. After adding CLASSPATH in environment java get classes from packages but still will generate errors on import command. You need to delete import algs4 and stdlib from source files and compilation will run smoothly.
This solution works on Ubuntu 12.04 with zsh.
Upvotes: 3
Reputation: 146
Is your Percolation program contained in its own package? If so try putting it into the default package by commenting out any package statements from your files and recompiling it.
Also, nothing in algs4
is in the java
package, it's all it's own separate thing.
Upvotes: 1
Reputation: 59617
If you're using Terminal to compile and launch your program, then in the Terminal window, begin by setting the CLASSPATH
:
$ export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar
Then you can type echo $CLASSPATH
and see that the jars are referenced.
Now, in the same Terminal window, use javac to compile your class. Setting the CLASSPATH
as above only applies to the current Terminal window and any processes launched from it.
Alternately you can pass the CLASSPATH
to javac:
$ javac -cp $CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar MyClass.java
To persist this CLASSPATH
for future Terminal sessions, add the export
line above to the file .profile in your home directory.
Upvotes: 5
Reputation: 1477
You probably have the classpath stuff right. The class you're trying to import may not be called java.stdlib
though. You need to import the fully qualified package name ... probably something like org.somecompany.ourlibrary.stdlib
. Thus you would need
import org.somecompany.ourlibrary.stdlib
at the top of your Percolations.java
file with the rest of the import statements.
Upvotes: 2
Reputation: 27880
Launch javac
with the -classpath <path_to_jar>
option. Or edit the CLASSPATH
environment variable so that it contains the JAR with the classes you wish to use.
Upvotes: 0