I'm usually working in Eclipse. In my program, I'm using this miglayout-4.0-swing.jar file from this source: link.
Somehere in the .jar file is class with MigLayout.
I use these imports:
import net.miginfocom.layout.Grid;
import net.miginfocom.swing.MigLayout;
//It's from the jar file.
In Eclipse i just add library:
Java Build Path -> Libraries -> Add JARs/Add external JARs -> path to miglayout-4.0-swing.jar
So in Everything working.
But I need to run it from terminal: java (I don't use packages so i use just classes from bin) but there is the problem with the .jar file, cause myMain class probably don't know where are the classes for that .jar (doesn't work the imports upper).
I tryed copy the .jar file to same directory where are the classes. Doesn't help.
What should I do to add the .jar file correctly?
Command line java command don't know where to look for the miglayout jar file. You should run in from command line like
java -cp path_to_miglayout_jar myMain
Pretty old question, but for the sake of completeness:
You need both . (current directory) and miglayout-4.0.jar to be on your classpath. You have two ways to do so. The easiest is to use -cp
In your case, you'll need to run:
java -cp "path_to_miglayout_jar/miglayout-4.0-swing.jar:." myMain
or if you work on a Windows OS:
java -cp "path_to_miglayout_jar/miglayout-4.0-swing.jar;." myMain
If unsure if you need to use a ; (colon) or a : (or whatever the OS is asking for), you can take a look at java.io.File.pathSeparator which contains the correct separator.
The other way would be to change your CLASSPATH variable.
Related
I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.
I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.
I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.
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.
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.
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.
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.
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.
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.
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.
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.
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!
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
.
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.
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
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
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.
I have a jar file which contains some classes those which i want to use in my project. I am working in command line and not in eclipse.
Please tell me how i should use those classes in the jar file for my project.
Use the -classpath command line option:
javac -classpath library.jar MyProgram.java
And then to run it, specify the classpath again - including where your actual code is:
java -classpath library.jar;. MyProgram
(That's assuming you're using Windows. On Unix use : instead of ; as that's the path separator.)
Jar file is a way to package java classes. To use the classes in the jar file, you need to include the jar in your classpath.
You need to then import the required class(es) in your java code and access them.
Of course you need to know how to use these classes i.e. what public methods etc. they expose.
If these jars correspond to some 3rd party library, you need to check the documentation (maybe on the web) to see how to use the classes.
You just include it to your classpath like this:
java -cp otherclasspath;thejar.jar yourlass