开发者

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

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:

  1. You have all the dependencies on your class path.

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜