Error "main class not found" - java

All,
My classpath has been set to the following folder:
CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project
All my java files and class files are under project folder listed in CLASSPATH.
Yet I am getting the following error while running a DriverClass in project folder:
C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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)
Could not find the main class: DriverClass. Program will exit.
Can anyone please help me understand the reason here?

You're inside the package. You should be sitting in the package root. Go one folder up.
cd ..
Then reexecute it using java project.DriverClass.
That said, you should prefer not to use the CLASSPATH environment variable. Yours is currently also invalid. There are spaces in unquoted pathnames. Also, the JDK/bin folder isn't supposed to go in the classpath. Just use the -cp argument like java -cp . project.DriverClass (while sitting in the package root folder).

Try:
C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass
assuming your package is project

After compiling if we get a main class not found error, It is simple try this
set classpath=%classpath%;.;
type this in cmd and run it..problem will be resolved

Related

Problems exporting Jar using Weka

i've got a big problem exporting my project as a jar file (inside my project i use weka). I'm using Eclipse.
I've added in Java Build Path weka.jar and i've attached to it weka-src.jar, testing my classifier under eclipse everything works, so i've decided to create a jar (because i need that another program use it).
First of all i've created the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ weka.jar weka-src.jar
Class-Path: .
Rsrc-Main-Class: start.Home
Main-Class: start.Home
The main class of the program is Home (inside the package start), while weka.jar and weka-src.jar are inside the /src/ directory of the eclipse program.
When i try to execute the jar created i receive this output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/Classifier
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: weka.classifiers.Classifier
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)
... 7 more
I think that the problem is the CLASSPATH so i've tried to solve it in this way:
I've created the Environment Variable CLASSPATH with this values:
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka.jar
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka-src.jar
but nothing changed, can someone help me please?
You are right: the classpath is the problem. Did you remove the items from the manifest classpath when you added them to the environment CLASSPATH? (Side note: place the source jar in the -sourcepath, not the classpath.)

Classpath Wildcards - Class Not Found Java

While running a scheduler program through command prompt, I am getting the below error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/ScheduleBu
ilder
Caused by: java.lang.ClassNotFoundException: org.quartz.ScheduleBuilder
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)
Could not find the main class: ScheduleEmail. Program will exit.
Even after adding all the jar files also I am getting the following.
Java command : java - cp .;E:/Jars/*; ScheduleEmail
Can anyone please suggest why I am getting this?
When using a wildcard (on windows platform), you need to surround it with quotes to work correctly:
java -cp ".;E:/Jars/*;" ScheduleEmail
1. http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html
first, make sure if there is a typo, there's no space between "-" and "cp"
second, check if quarts jars are in E:/Jars/

How to compile in Eclipse so I can use outside? (without opening eclipse)

I searched for an answer but didn't found it, so here it goes.
When I try to execute the program from cmd (with javac, I guess), I get this error:
D:\Libraries\workspace\bucky\bin\bucky>java apples
Exception in thread "main" java.lang.NoClassDefFoundError: apples (wrong name:bucky/apples)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I wanted to be able to send the application to my friends. I'm very new to this, so if it's a stupid question, it's because I only started a few days ago. I'm using java.util.Locale and java.util.Scanner if that is important. If I need to past the code, tell me. It's about 130 lines.
Thanks (If you noticed, I'm learning on Youtube, "thenewboston" is the user that made the turorials)
You should post how you try to run your code and the error message here as text. Just copy and paste from the Windows cmd box -- easy to do:
right click cmd window -> click select all -> press enter
and now it's copied to the clipboard.
It looks like you may be trying to run this without taking packages into consideration.
i.e., from the bin subdirectory, call `
otherDirectories/bin>java bucky.apples
But more importantly, if this were my project I'd have Eclipse export it first as a runnable jar file, and then simply run the jar file.
The menu order is:
Right-click on Project -> Export -> Java -> Runnable Jar File
That should be really simple. Eclipse got an export dialog. I think it was at File -> Export. There you can choose you desination for your compiled JAR and which external JARS should be inlcuded (e. g. JDBC).

How do I get started with JFreeChart?

I've never used any third party library before. What should I do after I downloaded jfreechart-1.0.14.tar.gz?
I don't know if I'm doing these things right:
1. Put the jcommon-1.0.17.jar and jfreechart-1.0.14.jar at the same directory as my source code.
2. Import needed class in the source code (e.g. import org.jfree.util.Rotation;)
Many articles tell you how to do this in IDEs. But instead of IDEs, I'm writing codes with vim and compile by myself. So, assume I didn't do any thing wrong, how should I compile the source code with javac and run the code with java?
Edit:
Here's my file layout:
./src
| - test.java
./lib
| - jcommon-1.0.17.jar
| - jfreechart-1.0.14.jar
I compile by
javac -cp "lib/*" -d classes/ src/test.java
then run by
java -cp classes:lib/jcommon-1.0.17.jar:jfreechart-1.0.14.jar test
However, some error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset
How can I resolve this problem?
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.jfree.data.general.PieDataset
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 6 more
The libraries shouldn't be at the same place as the source code. If you don't want to use a build tool like Gradle yet, which would handle your library dependencies, then I suggest using the following layout:
project
src
.java files here, organized in a folder tree matching the package tree
classes
compiled .class files here
lib
.jar files here
To compile, go in the project directory and execute the following command:
javac -cp lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar -d classes src/com/foo/bar/MyClass.java src/com/foo/bar/MyOtherClass.java
To run your app, execute the following command:
java -cp classes:lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar com.foo.bar.MyClass

com4j wrapper gen problems

I am trying to generate wrapper for com4j using tlbimp-20110320.jar. I also tried a 2008 version with the same errors. I get the same errors even when I try running against the example in the tut.
C:\Users\matthew\Documents\NetBeansProjects\test>"C:\Program Files (x86)\Java\jr
e7\bin\java.exe" -jar tlbimp-20110320.jar -o excel -p excel "C:\Program Files (x
86)\Microsoft Office\Office14\EXCEL.EXE"
Exception in thread "main" java.lang.NoClassDefFoundError: org/kohsuke/args4j/Cm
dLineException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.kohsuke.args4j.CmdLineException
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 6 more
After Iceman provided a link to a required library I was able to get it to run a little. However I had to copy the arg4j contents into the tlbimp jar, and the com4j contents into the tlbimp jar as well. Now I get
C:\Users\matthew\Documents\NetBeansProjects\test>"C:\Program Files (x86)\Java\jr
e7\bin\java.exe" -jar tlbimp-20110320.jar -o excel -p excel "C:\Program Files (x
86)\Microsoft Office\Office14\EXCEL.EXE"
Generating definitions from Excel
A referenced type library "Office" is generated into the same package "excel"
Unable to handle the type SAVEARRAY(Variant)*
method ConnectData
interface IRtdServer
A referenced type library "VBIDE" is generated into the same package "excel"
Exception in thread "main" java.lang.NullPointerException
at com4j.tlbimp.driver.Driver$1.resolve(Driver.java:64)
at com4j.tlbimp.Generator$LibBinder.<init>(Generator.java:382)
at com4j.tlbimp.Generator.getTypeLibInfo(Generator.java:686)
at com4j.tlbimp.Generator.getTypeName(Generator.java:663)
at com4j.tlbimp.TypeBinding.bind(TypeBinding.java:173)
at com4j.tlbimp.MethodBinder.declareReturnType(MethodBinder.java:542)
at com4j.tlbimp.MethodBinder.declareWithDefaults(MethodBinder.java:399)
at com4j.tlbimp.MethodBinder.declare(MethodBinder.java:118)
at com4j.tlbimp.InvocableInterfaceGenerator.generateMethod(InvocableInte
rfaceGenerator.java:39)
at com4j.tlbimp.InterfaceGenerator.generate(InterfaceGenerator.java:94)
at com4j.tlbimp.Generator$LibBinder.generate(Generator.java:491)
at com4j.tlbimp.Generator.generate(Generator.java:112)
at com4j.tlbimp.driver.Driver.run(Driver.java:107)
at com4j.tlbimp.driver.Main.doMain(Main.java:132)
at com4j.tlbimp.driver.Main.main(Main.java:56)
tlbimp-20110320.jar is required args4j-2.0.1.jar and com4j-20110320.jar
Note: com4j-20110320.jar, it is necessary to change the name to com4j.jar
Additional Info
The tlbimp of the latest version (tlbimp-2.1.jar), requires args4j-2.0.8.jar and com4j-2.1.jar.
In this version, file name change is not required.
From the stack trace, it looks like you are missing dependent jars from the java class path. The program seems to be looking for Koshuke's args4j library - http://args4j.kohsuke.org/.
Unjar tlbimp.jar and check out its manifest. It has a classpath where args4j and com4j must be in the same directory as tlbimp.jar and require specific names. The specifics of the name depend on the build and may or may not include version number. Name your com4j and arg4j as indicated and you're good to go.

Categories