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.
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 have a java Netbeans project having directory structure:
EMailQueue/
|-build/
|-classes/
|-mailqueue/
|-nbproject/
|-src/
|-mailqueue/
|-dist/
|-lib/
|-build.xml
|-maifest.mf
build/classes/mailqueue/ directory consists of all the classes compiled from all the .java files in src/mailqueue/ directory and dist/lib/ consists of all the .jar files (except JDK) imported in the application's files.
I want to know, how to run this application without using the jar file present inside the dist/ directory.
Also I want to know if I do not have classes files with me then how to compile .java files in src/mailqueue/ to generate class files in build/classes/mailqueue/ directory.
PS: Please give the exact command to run in terminal and optionally any explanation.
To compile Netbeans project You need to call Apache ant tool and pass them your build script file thorough -f parameter like this
ant -f ../apppath/build.xml
this will do full project with building jar file.
If You look at build.xml file You will see that it's only points to ./nbproject/build-impl.xml file where all required actions are actually defined.
Once no parameters passed to ant it calls default target
<target depends="test,jar,javadoc" description="Build and test whole project." name="default"/>
You see that this task depends of three other. Most interest is 'jar' target.
Look at this target - it depends of other 'compile' target
To call this target directly You need to pass this target to ant tool
ant -f ../apppath/build.xml compile
This will compile project but not to pack them to jar
ant -f ../apppath/build.xml run
will compile project and run main class
Hope this helps.
To compile Your code with pure java You need at first to run java compler in folder with all java files i.e call javac.
javac -cp {path to all java library files and external class files} *.java
Once You have all compiled with no errors run java same way and pass them Your main class name You want to run
java -cp {path to all java library files and external class files} ClassName
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 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.