This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello all, not sure if the captioned topic is considered as intermediate or not but here I go. I have set up my file structure on Windows as something like *.java goes inside /src dir; *.class goes inside /class dir; and the jar file goes inside the /dist dir.
I did the following to put the files into proper location like this: javac -d class src/MyClass.java jar -cvfe dist/MyClass.jar MyClass class/MyClass.class
Then when I try running using java -jar dist/MyClass.jar it can't find the main class??
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass Caused by: java.lang.ClassNotFoundException: MyClass 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)
However, if I don't use subdirectories then everything works fine. javac -d . src/MyClass.java jar -cvfe MyClass.jar MyClass MyClass.class java -jar MyClass.jar
Can anyone please help to clarify what I'm doing wrong or the correct to create jar files with class files inside some directory. Thanks.
-jar 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.