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.
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 have a very simple piece of code I am trying to run on the Windows command line (Windows 7). It runs in Eclipse fine.
I have read How to make javac find JAR files? (Eclipse can see them)
and
https://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html#Understanding
but clearly am missing something or misunderstanding something.
Here's the code:
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.common.notify.Notifier;
public class MakeUniqIDs {
public static void main(String[] args) {
for (int i=0; i<10; i++){
System.out.println(EcoreUtil.generateUUID());
}
}
}
When I try to compile it with javac I get following error message: "MakeUniqIDs.java:1: error: package org.eclipse.emf.ecore.util does not exist"
I am in the src directory where the above code lives, and used the following to attempt to compile it:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I put the jar files in the lib directory, and also tried putting the path to the eclipse plugins directory into the classpath, but still no go.
You have to specify external JAR on the classpath.
java -cp path/some.jar; etc.
You are using classes that are part of Eclipse itself. You could dig out their JAR, but that's generally not a good idea. generateUUID() seems to be used to create a UUID in source file based on the file content, which is an IDE feature.
First ensure that the jar is in that path, and the name is exactly the same ls ..\lib or dir ..\lib. Then use the command (with the right path):
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
Also note that you have imported org.eclipse.emf.common.notify.Notifier;, and that class is in the jar org.eclipse.emf.common not ecore, you can remove the line (since you are not using it) or add the jar separated with ;.
Example:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar;..\lib\org.eclipse.emf.common_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I've downloaded JAR archive from here. I want to know which class is the main. I checked Manifest file for main class specification Main-Class: com.some.className but it's not there. Where else could the main class be specified?
OkHttp is meant to be used as a library. One of the easiest ways to build java programs that need library dependencies is to use Maven. In the absence of Maven or other tools you can get the program running by copying the jar to a folder. You will also need to download okio.
To try the library out, create a java file in the same folder that contains the 2 jars and call this file GetExample.java . Paste the example source from the OkHttp site.
To compile the program, open a terminal/command prompt window and go to the location where you have GetExample.java and the jar files. Run the command:
javac -cp okhttp-2.1.0.jar GetExample.java
This should compile the file and create GetExample.class for you.
To run the program use the command:
java -cp okhttp-2.1.0.jar:okio-1.0.1.jar:. GetExample
or, if you are using windows (replace : with ;):
java -cp okhttp-2.1.0.jar;okio-1.0.1.jar;. GetExample
You should see the content of a README.md file.
I hope this helps.
Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?
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.