Need Help in the Creation of Jar File through Java Command? - java

I need to create jar file of my java app. For that first i had created a file called Manifest.MF , in that file i had stored the following code
Manifest-Version: 1.0
Main-Class: com.demo.test.JavaTest
and then i had exceuted the following command in command prompt
jar cvfm JavaAppDemo.jar MANIFEST.MF "C:\JavaSamples\MyApp"
where MyApp is my project directory, there i had created packages and used other jar files too
and then i try to run the jar using java command
java -jar JavaAppDemo.jar
and i got following Exception
Exception in thread "main"
java.lang.NoClassDefFoundError:
com/demo/test/JavaTest
Caused by:
java.lang.ClassNotFoundException:
com.demo.test.JavaSamp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native
Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class:
com.demo.test.JavaTest. Program will
exit.
pls tell me, i want to create a jar and i have to bundle the files in my project directory to that jar files, when i extracted the jar i got my project folder, but when i run my jar i got the above exception so i got confused where the loop hole is present.

You should probably use this syntax for building your jar :
jar cvfm myapp.jar myapp.MF -C classes myclasspath

Okay well a few things:
Here is how you should run your application:
java -classpath myapplication.jar:.. -Dlog4j.info -Dlog4j.configuration=file:../../conf/log4j.xml com.mywebsite.Benchmarks
Take that log4j stuff out if you don't need it. I am sure you are just missing the "com.mywebsite.Benchmarks" line which tells the jar which compiled class to start with. If you have Benchmarks, FullTest, MainApp all compiled in your jar and the main method is in Benchmarks, you need to tell the jar that.
As far as building your jar...
It is the easiest to use some kind of ant build script.
<target name="build">
<delete file="MyApplication.jar" />
<delete dir="_classes" />
<mkdir dir="_classes" />
<javac srcdir="src" destdir="_classes" debug="true">
<classpath path="../../3rdParty/log4j/log4j-1.2.16.jar" />
</javac>
<jar jarfile="MyApplication.jar" basedir="_classes"/>
</target>
That should get you started. But you need to make sure of a few things when your run it:
You have all the dependencies on your class path.
You tell the jar which class to start with.

Some things I can think of:
The error message says that it can't find the file com/demo/test/JavaTest.class. You could try looking at your jar file with a zip file program (a .jar file is compressed just like a .zip file) to see if that file is really not in there. I suspect the error code is actually correct in that it is not actually there, but at least you can see it (or not see it) with your own eyes. And remember that it has to begin with a "com" folder.
Perhaps you are confusing .class files and .java files. Are you sure your .java files are actually compiled into .class files? This might be unlikely, but it may as well be said.
Maybe you need to run your jar command with "C:\JavaSamples\MyApp\*" at the end to pick out all the files in the right directory. Maybe you were telling the jar command to include the folder "MyApp". You don't actually want that folder; you want all the files inside it.

Related

CMake add_jar with INCLUDE_JARS not working

I am using CMake to compile jar file with add_jar command. Problem is that when I try to add INCLUDE_JARS to specify dependency to external jar, the code will not run. Here is the code example:
add_jar(testJar
SOURCES
sources/com/test/Main.java
INCLUDE_JARS
${CMAKE_SOURCE_DIR}/extern/org.json/json-20171018.jar
ENTRY_POINT com.test.Main
)
Running the testJar with "java -jar testJar.jar" gives me the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
The org.json jar should be in classpath, right? Adding manifest file with classpath solves the problem but is there way to do this without the manifest file?
And yes, I would use maven or gradle for building but as of restrictions in the project I cannot do that :)
The option INCLUDE_JARS only makes sure that the given external jar is added to the class path upon compilation of the given Java source files. The jar file is neither copied to the build directory nor is it added to the jar file generated by add_jar.
If inclusion of the external jar in the Manifest file is not an option for you, manually add it to the class path upon running the testJar, i.e.:
java -cp path/to/json-20171018.jar -jar testJar.jar

OS X app from jar: could not find or load main class when launched

Upon trying to launch an OS X app I created from a jar file, nothing happens. When I then look into the .app contents and run the JavaAppLauncher, the terminal displays "Error: Could not find or load main class franky.GUI".
The steps I took are the following:
1) I created an executable jar file of my project by using eclipse. The package is named "franky", so when I unarchive the jar file again, there is a folder called franky with the java classes (including GUI.class, which is the main class). The external jars are also present in the top level directory.
2) I created a new folder with a build.xml file, and also the structure dist/frankenbot.jar and lib/appbundler-1.0.jar
3) the ant build file is created following this tutorial.
<property environment="env" />
<taskdef name="bundleapp"
classname="com.oracle.appbundler.AppBundlerTask"
classpath="lib/appbundler-1.0.jar" />
<target name="bundle-franky">
<bundleapp outputdirectory="dist"
name="Franky"
displayname="Franky"
identifier="Franky"
mainclassname="franky.GUI">
<runtime dir="${env.JAVA_HOME}" />
<classpath file="dist/frankenbot.jar" />
<option value="-Dapple.laf.useScreenMenuBar=true"/>
</bundleapp>
</target>
4) I would also like the final app to include JRE, so I made a bash_profile file containing the line 'export JAVA_HOME=$(/usr/libexec/java_home)'. Before building, I execute 'source .bash_profile' in the terminal.
5) Finally, I bundle the app using 'ant bundle-franky'.
The build is succesful and the app is created in the 'dist' directory. But as mentioned above, nothing happens when I click it, and running the JavaAppLauncher gives "Error: Could not find or load main class franky.GUI". I have searched a lot, and I also tried different mainclassnames/identifiers and such, just in case; but nothing seems to work. Any help would be greatly appreciated!

what is correct internal structure of JAR file

This is a really silly question that I can't fine a difinitive answer to.
Background.
I'm using Eclipse (with one of the ANT plugins, on an XP terminal).
I have just started playing with ANT, in the [jar] directive I am setting the location of my finished JAR file and I get the following when I 'unzip' the file
META-INF/MANIFEST.MF
MyMainFile.class
which is consistent with that found on the oracle web site for the internal structure.
(here http://docs.oracle.com/javase/tutorial/deployment/jar/view.html )
But when I try to run my file I get a 'main class not found' error ?
I have seen some other posts where people have 'unzipped' the JAR file and ended up with a structure of the following...
META-INF/MANIFEST.MF
dtvcontrol/DTVControlApp.class
(from here http://www.coderanch.com/t/528312/java/java/standalone-application)
So should I get a structure where my class files are in a directory that reflects the name of the package... eg
META-INF/MANIFEST.MF
MyPackage/MyMainFile.class
and if so, why am I getting the incorrect structure using ANT, or why are there 2 different 'correct' internal structures? (how to specifify main-class and classpath for each / control what I get)
Also in case you are interested, in the MANIFEST file states (build using ANT)
[attribute name="Main-Class" value="MyPackage.MyMainFile"/]
Also the directory structure of the package under development is as follows...
/JavaDev/MyTestPackage/src (contains the source files)
//JavaDev/MyTestPackage/bin (contains the class files from eclipse, or from the ANT JAVAC task, have I named it incorrectly? should I have called it build ? )
Further to this, when I create the jar I am not naming it 'MyTestPackage.jar' but simply 'test.jar' could this be causing a problem? I assume not as if I have well understood that is what the [main-class] definition stuff is all about.
Further to all this...
'MyTestPackage' is actualy a small visual error messaging library that I use elsewhere, and I have a second file that has a main class to use for testing. As such it points to various libraries (do I need to copy all the SWT libraries to a specified directory?)
I have read somewhere that if I load libraries into my main class (which I obviously do to open the window) then trying to run the program will fail on a 'main class not found' if I use them, same is also true for adding in any 'static final' members (which I use for the loggin of errors).
'Static Final' problem...
I tried to adjust the classpath in ANT, and I get a load of other errors for the connection to Log4J and my 'wrapper' that I use (to do with it being a bad import), but the libraries exist where they should as set in the classpath).
I feel like I am almost there.... but not quite...
I'm doing this for the small 'library projects' that I am creating with the intention of using MAVAN for the main outer package that will connect them all together... for now however I just want to get this going so as it works.
I can supply the full source, or any other info as required.
Thanks in advance...
David
It's simple when you know where to look. Say your META-INF/MANIFEST.MF contains the line:
Main-Class: mypackage.MyMainFile
then the structure of the jar needs to be
META-INF/MANIFEST.MF
mypackage/MyMainFile.class
where MyMainFile has to be in the proper package:
package mypackage;
public class MyMainFile {
public static void main(String[] args) {
...
Your error message is caused by MyMainFile being in the wrong place.
Edit: it's been a while since the last time i did that with ant, but i think you need something like this: a source file structure that reflects the package struture, say
src/main/java/mypackage/MyMainFile.java
and a directory to put the compiled class file into, say
target
(I'm using maven conventions here, ant doesn't care and you can use the (rightclick)->properties->Java Build path->Sources tab in eclipse to set the source dir to src/main/java and the target to target/classes). Then in ant, have a compile target that compiles from source to target:
<target name="compile">
<mkdir dir="target/classes"/>
<javac srcdir="src/main/java" destdir="target/classes"/>
</target>
so that after ant compile you should see the class file in the target
target/classes/mypackage/MyMainFile.class
Then have a ant jar task that packages this:
<target name="jar" depends="compile">
<jar destfile="target/MyJarFile.jar" basedir="target/classes">
<manifest>
<attribute name="Main-Class" value="mypackage.MyMainFile"/>
</manifest>
</jar>
</target>
After saying ant compile jar you should have a file MyJarFile.jar inside target and
java -jar MyJarFile.jar
should run the main method.

How to create executable .jar file with netbeans

I'd like to make "double-click" cli application but still don't get how.
I know I should propably somehow edit manifest but that is all. I googled ofc. but no success.
Thanks for any tips.
Here is the output from build, run, and manifest:
compile:
Created dir: /home/nick/NetBeansProjects/SemestralWork/dist
Building jar: /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "/home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar"
jar:
BUILD SUCCESSFUL (total time: 1 second)
java -jar /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: semestralwork.Main. Program will exit.
MY MANIFEST created with build:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.0-b08 (Sun Microsystems Inc.)
Main-Class: semestralwork.Main
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
These two lines tell you all you need to know:
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
And a further clue is dropped by the manifest output:
Main-Class: semestralwork.Main
This means that the JAR file is looking for a package named semestralwork and a class named Main inside it. It fails at this point because it cannot find either the semestralwork package or the Main class.
As you pointed out in your question, the problem is indeed in the manifest file. You can edit this directly in your JAR file if you like, but a better idea would be to do this from Netbeans:
Click on `File --> Project Properties (semestralwork)'
In the dialog that opens, on the tree on the left select Run
Then, on the right, under the field labeled Main class:, enter the fully qualified class name of the class that you want executed when run from the command line.
In your case, as I see from your comment on #Aaron's answer, if your main class is in a file called encryption.java, and it is in the default package (no package), just enter encryption.
Once this is done, do a clean and build, then try running it from the command line again.
HTH
Since I encountered the same problem, I can clarify the solution a little bit.
You must create main Java class outside your method (for example-default_package folder), and then invoke your method(folder), e.g import your_folder.connected_class; in that main class.
Hopefully I could help someone with the same problem.
It's easier to make a .exe from a .jar without netbeans.
Here are my suggestions:
1. Use a special application for this(ex: JSmooth, JEXECreator etc)
2. Make a C++ program that starts a JVM (see this tutorial)
The default class search path may be the issue. You should try changing directory to the location the jar is and launching with java -jar Semestral.jar. Also you may have misnamed the main class. Please also include your package structure.
Open the JAR file with a ZIP tool (or try less ... if you're on Linux or jar tvf ...). Make sure there is a directory semestralwork in there which contains a file Main.class.
It's Easy. Download a copy of netbeans. Make a new project in netbeans. Goto your main class in the Projects Explorer. This should be folder "YourProject" As the class YOURPROJECT.JAVA . It is this yourproject.java file that you want to start with. Just write your code into the public static void main area. You can run the program with the green play button in the top toolbar.

NoClassDefFoundError while running from jar

I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). The classes the JVM is complaining about are my classes (no third party jars required). When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about.
The jar is created using the following task:
<target name="jar" depends="">
<jar destfile="build/app.jar" >
<manifest>
<attribute name="Built-By" value="hamza"/>
<attribute name="Main-Class" value="com.hamza.driver.ui"/>
<attribute name="Class-Path" value="./"/>
</manifest>
<fileset dir="build">
<include name="**/*.class"/>
<include name="**/*.png"/>
<include name="**/*.xpi"/>
<include name="**/*.html"/>
<exclude name="**/*.jar"/>
</fileset>
</jar>
I cannot figure out what is causing the problem. If I unzip the jar and run the jar from the directory I unzipped the class to, everything works fine. So, I am assuming all the required files are inside the jar.
EDIT: com.hamza.driver.ui is a class in a package called com.hamza.driver which has main.
After the build, I get one jar "app.jar", and I run it using "java -jar app.jar", which executes fine on OS X, but not on Windows.
If I unzip app.jar in a seperate directory and run "java -jar app.jar", it excutes fine.
EDIT 2: exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hamza/gui/tr
ansfer/ClipboardTransferHandle
at com.hamza.driver.ui.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.hamza.gui.transfer.Clipboa
rdTransferHandle
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
ClipboardTransferHandle .class files are present in the jar.
EDIT 3: imports for the clip board class:
import java.util.logging.Logger;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.IOException;
While playing with it, I found that if I try to declare ClipboardTransferHandle as a static variable in the driver, it works, but every object that is not static is not found.
All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; everything that is created not static causes NoClassDefFound, but if I declare them static for testing, they work.
This is the problem that is occurring,
if the JAR file was loaded from "C:\java\apps\appli.jar", and your manifest file has the Class-Path: reference "lib/other.jar", the class loader will look in "C:\java\apps\lib\" for "other.jar". It won't look at the JAR file entry "lib/other.jar".
Solution:-
Right click on project, Select Export.
Select Java Folder and in it select Runnable JAR File instead of JAR file.
Select the proper options and in the Library Handling section select the 3rd option i.e. (Copy required libraries into a sub-folder next to the generated JAR).
Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar
Now what will happen is the class loader will look in the correct folder for the referenced JARS since now they are present in the same folder that contains your app JAR..There is no "java.lang.NoClassDefFoundError" exception thrown now.
This worked for me... Hope it works you too!!!
Which class is missing? Your Main-Class attribute looks a little suspect--is com.hamza.driver.ui a class or a package?
There is a chance, that the NoClassDefFoundError (I really hate this error - always drives me crazy...) isn't thrown because it doesn't find the class it tells you (-> your class) but because java can't find one of the classes that are used to instantiate that class.
I had this problem once, when a class imported another class from a different jar (in my case: an OSGi bundle) which hadn't been properly exported. Although this was a OSGi specific problem - You may have the same problems in your environment. Maybe your application depends on some classes that are present in your actual OS-X environment but not in the actual Window environment. I'm not looking at third-party libraries but at the Java implementations itself.
Good luck!
Edit
There are two more quite similar question on SO, unfortunately with no accepted solution, but maybe one of the hints over there can help in your case:
NoClassDefFound when running a jar
NoClassDefFoundError inside jar
Edit 2
Here's a similiar problem that has an accepted answer. Hope this one helps:
NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?
Have you specified the new jar in your classpath (java -cp .;new.jar MainClass.class)?

Categories

Resources