Troubles with executable jar files!
I`ve created a jar file with the folowing statement:
jar vcmf mainClass chart.jar chart/*.class
where mainClass is a file with the unique line: Main-Class: chart.InterfaceTeste
When I try to run my application I get this errors:
Exception in thread "main" java.lang.NoClassDefFoundError: chart/InterfaceTeste (wrong name: InterfaceTeste)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
The Main Class Name "InterfaceTeste" is rigth, but it continous get me this error. What have I to do?
> where mainClass is a file with the unique line:
> ne: Main-Class: chart.InterfaceTeste
>
The Main-Class line above specifies that InterfaceTeste is in a package called chart. The InterfaceTest source code must start with "package chart;"
> When I try to run my application I get this
> his errors:
> Exception in thread "main"
> in" java.lang.NoClassDefFoundError:
> chart/InterfaceTeste (wrong name: InterfaceTeste)
The error message says that the JVM was looking for a class named chart.InterfaceTeste and found an InterfaceTest.class file in a chart directory, but inside the file was a class named InterfaceTeste, not chart.InterfaceTeste. In other words, the source code for the .class file did not start with "package chart;"
Then, do I need to create a package to run jar files? if not, what do I have to do to run it?
> Then, do I need to create a package to run jar
> files? if not, what do I have to do to run it?
It is not required to create a package to run jar files. It is required to specify class names correctly, and it is required to store .class files in the correct directory structure.
In your case, it looks like it would be easiest to change the manifest Main-Class to InterfaceTeste, then "cd chart" and "jar vcmf mainClass chart.jar *.class"
Ok, it worked, but I have troubles with some classes that I need, like JDBC and JFreeChart, I get errors like:
Exception in thread "main" java.lang.NoClassDefFoundError:oracle/jdbc/driver/OracleDriver
at DataBaseConn2.<init>(DataBaseConn2.java:17)
at ConfereDados.<init>(ConfereDados.java:10)
at InterfaceTeste.<init>(InterfaceTeste.java:38)
at InterfaceTeste.main(InterfaceTeste.java:321)
Do I have to put these classes into my .jar? If yet I have a path to these classes on my computer, do I have to put into .jar file yet?
Can I use jar files into jar files? For example: I use some JFreeChart classes, and they are into jar file, but I want use them into my Myfile.jar. What do I do?
> Ok, it worked, but I have troubles with some classes
> that I need, like JDBC and JFreeChart, I get errors
> like:
>
> Exception in thread "main"
> "main" java.lang.NoClassDefFoundError:
>oracle/jdbc/driver/OracleDriver
> at
>at DataBaseConn2.<init>(DataBaseConn2.java:17)
> at ConfereDados.<init>(ConfereDados.java:10)
> at
> at
>at InterfaceTeste.<init>(InterfaceTeste.java:38)
> at
>at InterfaceTeste.main(InterfaceTeste.java:321)
>
> Do I have to put these classes into my .jar? If yet
> et I have a path to these classes on my computer, do
> I have to put into .jar file yet?
You do not have to put those into the same jar. You might not have those jars in your Classpath - it depends on how you launch the jar. If you use "java -jar ..." then you should add a Class-Path in the jar manifest to tell the JVM where to find the other jars.
> Can I use jar files into jar files? For example: I
> use some JFreeChart classes, and they are into jar
> file, but I want use them into my Myfile.jar. What do
> I do?
Java does not directly support jar files inside jar files. You would need to write your own code if you want to use jars inside jars. It is my understanding that the way this is normally done is via the Class-Path in the manifest of the executable jar.
No issue with the Class-Path in manifest -- check it out :
Note: The Class-Path header points to classes or JAR files on the local file system, not JAR files within the JAR file or classes on the network. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
<see : http://java.sun.com/docs/books/tutorial/jar/manifest/downman.html>
But now, what king of codes, that's all the question ?
For myself, I have a jdbc jar embedded in another jar whitch used it... Maybe with ClassLoader ?