Creating a exc jar file in eclipse ? - java

so I created a jar file, clicked on export and etc
but when I double click it doesnt do anything
I dont see my classes in the folder
all i see is
junit
org.hamcrest.core_1.1.0.v2009050107100…
what am i doing wrong ?

If your jar is a valid executable
then this should work
java -jar your.jar

Jar files don't aren't always run by double-clicking.
In order to run the jar file, run the command java -jar yourjarfile.jar while in the same directory as the jar file. This assumes that your PATH system variable is set properly.
If you receive an error such as "Failed to load Main-Class manifest attribute from yourjarfile.jar" it means that there is no main class defined in your jar file, and the java interpreter doesn't know where to start.
You can make sure that a main class is specified in Eclipse by exporting as a "Runnable JAR file" and making sure you select a launch configuration that you use to run your program.
In order to list the contents of the jar file, run the command jar tvf yourjarfile.jar while in the same directory as the jar file.
In order to extract the contents of the jar file, run the command jar xvf yourjarfile.jar while in the same directory as the jar file.

Related

ERROR: no such file or directory trying to create jar file from terminal

I have performed the following steps:
A single java file called ComputeArray.java, with no specified
package
Navigated into the folder containing the program
Created the class file (and it runs)
The command I am having trouble with is as follows:
jar cvf computearray.jar ComputeArray.class
And I have tried using a manifest with cvmf and also tried adding ComputeArray.java at the end. I always end up with the same error "no such file or directory
You also have to specify the manifest file while creating jar file, something like:
$ jar cmf Hello.mf Hello.jar Hello.class Hello.java
Here is great documentation to Distributing your Application as an executable JAR file: https://introcs.cs.princeton.edu/java/85application/jar/jar.html

How to create an executable jar file which can be distributed or packaged?

I am using getdown to create a means to update a java application.
When I have completed this tutorial, I tested if it works on command line as below:
% java -jar c:/downloads/getdown-X.Y.jar c:/netBeans/getdown/src
Thankfully, this works and launches the application. Great.
How do I make a jar file and distribute this?
I tried to make a jar file on this project but it didn't work, this project does not run. When I run this getdown-X.Y.jar on command line.
I think it still using the same file which I created before c:/netBeans/getdown/src. Eventually, it is failing to execute since it is missing the jar file. So, how to make this project into a jar file and distribute it.
I am not sure what OS you are working on.You can do this by creating an executable jar file. Please follow the steps here:
If you want to create a jar file with additional file. Here in below, if you want to create a jar file of imagine src.class with additonal text file with it which is readme.txt
c:\patel\projects\netbeans\getdown\src.class
c:\patel\projects\readme.txt
Run this command: jar -cvfm src.jar readme.txt netbeans\getdown\*.class
which is: c:\patel\projects\jar -cvfm src.jar readme.txt netbeans\getdown\*.class
Now your executable jar file is ready. To run this jar file:
run this on command prompt: java -jar src.jar

Jar file won't run via command prompt or double-click

I recently finished a project which works as it is supposed to in my Eclipse IDE as both multiple files and as a single file.
Eclipse exports the jar file and only makes noise about the warnings.
When I go to run the jar file with a double-click, the cursor seems to flash to a hourglass for less than a second and then nothing. When I try to run the jar file from the command line with java -jar myJarFile.jar the command prompt window seems to wait a second and then brings the file path line and cursor with no errors and no other messages.
I have double checked both my Path variable and that I have the latest version of Java installed.
Any suggestions?
Check this thread: Failing to execute the jar file using java -jar command
Basically, what you need to do is when you run the jar, you need to specify the class to be executed in the command line, like so:
java -cp test.jar com.app.ClassName
Did you just make a jar file, or a runnable jar file? If you only did the former then it will not execute on a double click. So it's Right Click the project in Eclipse -> Export -> Runnable JAR File -> Choose the appropriate Launch Configuration -> Choose where you want it to go.
Confirm that your application code is in the jar file:
jar tf myJarFile.jar
Confirm that your application's main class is listed in the Main-Class attribute in the manifest, and that any other jar files needed by your application are listed in the Class-Path attribute:
jar xf myJarFile.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF
Try running with verbose logging of classes being loaded; this should show if some necessary class can't be found:
java -verbose:class -jar myJarFile.jar

trying to run a .jar file in command prompt

I am trying to run a jar file using my command prompt (Windows XP) but get NoClassDefFoundError.
I have my DateAndTime.class file in a folder called dateandtime and also indicated a package called dateandtime in the source file.
Outside the folder I have a manifest.mf file with specification
Main-Class: dateandtime.DateAndTime
I put this in the command file
jar cmf manifest.mf myJarFile.jar dateandtime
and this creates the myJarFile.jar in the same folder as manifest.mf.
When I try to run this jar file however I get the NoClassDefFoundError
java -jar myJarFile.jar
If I jave all the classes in the same directory with no package specified then the .jar file runs fine but as soon as I try to specify a package, even though myJarFile.jar was created I get the error.
Why is that?
Regards
If you are getting a NoClassDefFoundError it means a class that was present during compilation of your classes is absent during their execution. Which implies that you JAR file does not contain all the dependencies requires by your classes in order to run. Since your question is lacking detail on your project structure I can only recommend you revisit your application's dependency tree and determine all the classes that need to be included in the JAR.
Syntax:
java -jar fileName.jar
Example:
java -jar myfile.jar

Java creating .jar file

I'm learning Java and I have a problem. I created 6 different classes, each has it's own main() method. I want to create executable .jar for each class, that is 6 executable .jar files.
So far I tried
java -jar cf myJar.jar myClass.class
and I get 'Unable to access jarfile cf'. I'm doing something wrong but I don't know what. I'm also using Eclipse IDE if that means something.
In order to create a .jar file, you need to use jar instead of java:
jar cf myJar.jar myClass.class
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class header (e.g., Main-Class: myClass).
However, as Mark Peters pointed out, with JDK 6, you can use the e option to define the entry point:
jar cfe myJar.jar myClass myClass.class
Finally, you can execute it:
java -jar myJar.jar
See also
Creating a JAR File
Setting an Application's Entry Point with the JAR Tool
Sine you've mentioned you're using Eclipse... Eclipse can create the JARs for you, so long as you've run each class that has a main once. Right-click the project and click Export, then select "Runnable JAR file" under the Java folder. Select the class name in the launch configuration, choose a place to save the jar, and make a decision how to handle libraries if necessary. Click finish, wipe hands on pants.
Often you need to put more into the manifest than what you get with the -e switch, and in that case, the syntax is:
jar -cvfm myJar.jar myManifest.txt myApp.class
Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include.
Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right place within the jar file.
way 1 :
Let we have java file test.java which contains main class testa
now first we compile our java file simply as javac test.java
we create file manifest.txt in same directory and we write Main-Class: mainclassname . e.g :
Main-Class: testa
then we create jar file by this command :
jar cvfm anyname.jar manifest.txt testa.class
then we run jar file by this command : java -jar anyname.jar
way 2 :
Let we have one package named one and every class are inside it.
then we create jar file by this command :
jar cf anyname.jar one
then we open manifest.txt inside directory META-INF in anyname.jar file and write
Main-Class: one.mainclassname
in third line., then we run jar file by this command :
java -jar anyname.jar
to make jar file having more than one class file : jar cf anyname.jar one.class two.class three.class......
Put all the 6 classes to 6 different projects. Then create jar files of all the 6 projects. In this manner you will get 6 executable jar files.

Categories