I am very new to java and tried to run a simple code of calculating volume. The code is below:
package chapter6;
class Box {
double width;
double height;
double depth;
}
package chapter6;
public class BoxDemo {
public static void main(String[] args) {
Box myBox = new Box();
double vol;
myBox.depth = 1;
myBox.height = 2;
myBox.width = 3;
vol = myBox.depth * myBox.height * myBox.width ;
System.out.println("Volume: " + vol);
}
}
I am able to run the code from eclipse, but when i try to run the code in Command Prompt i get the error:
C:\Prabhjot\Java\CompleteRefence\build\classes>java BoxDemo.class
Exception in thread "main" java.lang.NoClassDefFoundError: BoxDemo/class
Caused by: java.lang.ClassNotFoundException: BoxDemo.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: BoxDemo.class. Program will exit.
Please assist.
First class file should be at this location:
C:\Prabhjot\Java\CompleteRefence\build\classes\chapter6\BoxDemo.class
Then you should be inside:
C:\Prabhjot\Java\CompleteRefence\build\classes>
Then issue the command:
java chapter6.BoxDemo
You have put your class in a package called chapter6. This means that the java file should be in a folder called chapter6 in the class root folder of your project. And when you run it, you should be in the root folder and use the command java chapter6.BoxDemo
There is mistake in how you are running your program from console.
You are doing
java BoxDemo.class
But you need to do only
java BoxDemo
While running your program you don't need to mention .class with the name.
and if you are accessing it from root folder then you need to do
java chapter6.BoxDemo
try this
C:\Prabhjot\Java\CompleteRefence\build\classes>java chapter6.BoxDemo (RUN)
There is no need to specify .class extinction to the file while running.After compiling the java file it will create the .class file.
EXAMPLE
When you invoke BoxDemo.class, Java looks for a class called class in the BoxDemo package, which doesn't exist. As you can see from the output java.lang.NoClassDefFoundError: BoxDemo/class, it's searching for a directory BoxDemo.
Instead, just specify the class name: BoxDemo; e.g. java BoxDemo.
Related
So far I have downloaded Apache Commons library , extracted the library
commons-lang3-3.8.1.jar in Java\jdk1.8.0_172\jre\lib\ext.
Now I have created a class with two fields and I want to compare two objects using
ob1.equals(ob2). Method equals and hashCode have been overridden and the error I'm getting is Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/builder/EqualsBuilder at runtime.
import java.util.*;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
class key{
int end;
LinkedList<Integer> via = new LinkedList<>();
key(int x,LinkedList<Integer> ob){
this.end = x;
this.via = ob;
}
#Override
public int hashCode(){
return new HashCodeBuilder().append(end).append(via).toHashCode();
}
#Override
public boolean equals(Object obj)
{
if(!(obj instanceof key))
return false;
if(this==obj)
return true;
key o=(key)obj;
return new EqualsBuilder().append(end,o.end).append(via,o.via).isEquals();
}
}
class main{
public static void main(String[] args)
{
key ob1 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));
key ob2 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));
System.out.println(ob1.equals(ob2)); //expecting true
}
}
The details of the error are given below.
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/lang3/builder/EqualsBuilder
at key.equals(test.java:29)
at main.main(test.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.builder.EqualsBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
I have been facing this issue for a long time. I have checked all the class files and I'm quite sure that the libraries are loaded properly but I don't know why I'm getting NoClassDefFoundError at the runtime.
After spending hours on this issue I finally fixed it by setting the CLASSPATH variable.
I tried using -cp command but unfortunately that didn't work for me. If we do this explicitly, then you don't need to supply a "-cp" or "-classpath" switch value to the java compiler and java interpreter, since they will already know the updated classpath.
On my windows machine, I have set the CLASSPATH variable via the following:
set CLASSPATH=/coding #October\lib\commons-lang3-3.8.1.jar;.
Currently, I'm in coding #October directory. The commons-lang3-3.8.1.jar file is located in the coding #October\lib directory.The myapp.java file is located in the coding #October directory.
After setting the classpath, I can then compile and execute myapp.java via
javac myapp.java command directly and then java myapp to execute the script.
You placed the jar in the correct jre\lib\ext relative path... but it will work only if the java command you run comes from the jre\bin directory of the same jre path where you did the change.
If you copied the correct jar in the extension directory but you get this exception it very probably means that as you run your program you don't use the JRE where you did the change but another one.
The java command from the PATH env variable very probably doesn't refer to the JRE you extended. You can display PATH in your shell to check that.
So either set the PATH with the java home path of the JRE you extended or just run the java command by specifying the absolute path such as /foo/jre/bin/java main.
It should (to not say has to) work.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at io.appium.java_client.internal.ElementMap.getElementClass
Answer: add selenium jar "commons-lang3-3.8.1" for resolving this issue
Background:
I am trying to use ini4j for the purpose of parsing config files in the ini format.
I run the command:
javac -classpath ini4j-0.5.4.jar Driver.java
and the compilation goes smoothly...however when I attempt to run the program running:
java Driver
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/ini4j/Ini
at Clock.main(Clock.java:13)
Caused by: java.lang.ClassNotFoundException: org.ini4j.Ini
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
From what I understand this error is a product of the jvm being able to find the class file being referenced in the error and failing. I checked the ini4j jar file and confirmed that the Ini.class file does in fact exist. Can anyone please tell me what am I missing here ?
The following is my source code:
import java.util.Map;
import java.io.IOException;
import java.io.File;
import org.ini4j.* ;
public class Driver{
public static void main(String[] args){
System.out.println("running in Clock-J main()");
String fileName = "./test.ini" ;
File fileObject = new File(fileName);
try{
Ini ini = new Ini(fileObject);
}catch(IOException e){
String exception = e.toString() ;
e.printStackTrace();
}
}
}
You also need to specify the -classpath option when you run the program. Java will not automatically pick up the jar files that you passed when you compiled your code.
The correct answer for my situation appears to be:
java -cp .:ini4j-0.5.4.jar Driver
Thanks for pointing me in the correct direction guys!!!! Using an IDE causes one to forget stuff like this. I really appreciate it!!!! Thanks!!!!!
Look at Elipse->Properties->JavaBuildPath->Libraries
list of extended JARs have to be in CLASSPATH
please check for more info
I'm trying to use javacpp and encounter some difficulties with eclipse (+ mac OS).
When I run this in my command line - it works fine:
#include <string>
namespace LegacyLibrary {
class LegacyClass {
public:
const std::string& getProperty() { return property; }
void setProperty(const std::string& property) { this->property = property; }
private:
std::string property;
};
}
and
import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;
#Platform(include="LegacyLibrary.h")
#Namespace("LegacyLibrary")
public class LegacyLibrary {
public static class LegacyClass extends Pointer {
static { Loader.load(); }
public LegacyClass() { allocate(); }
private native void allocate();
public native #ByRef String getProperty();
public native void setProperty(String property);
}
public static void main(String[] args) {
LegacyClass l = new LegacyClass();
l.setProperty("Hello World!");
System.out.println(l.getProperty());
}
}
as suggested in this post
I get the end result: "hello world".
Now I'm taking this into my eclipse IDE and for some reason, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: com.test.LegacyLibrary
at com.googlecode.javacpp.Loader.load(Loader.java:293)
at com.googlecode.javacpp.Loader.load(Loader.java:271)
at com.test.LegacyLibrary$LegacyClass.<clinit>(LegacyLibrary.java:12)
at com.test.LegacyLibrary.main(LegacyLibrary.java:23)
Caused by: java.lang.ClassNotFoundException: com.test.LegacyLibrary
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 sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.googlecode.javacpp.Loader.load(Loader.java:291)
... 3 more
I placed the 2 files into a package com.test and the javacpp.jar I placed in the classpath.
Is there anything I'm missing here?!?! the error message indicates that something in the classpath is incorrect (or missing), but what is it? (I also tried to point in the properties to the javacpp.jar in a different folder but I get the same error message). Maybe VM arguments?!?!
Thank you!
Did you add the javacpp.jar file to the projects libraries? Go to the project properties, then the Build Path information, there should be a tab for Libraries. Add an External Jar file, and select the javacpp.jar file.
Go to Project Properties->Java Build Path, in the tab Source click on the arrow to expand the source folder, then select Native Library Location and click on Edit, After all find the path to the LegacyLibrary.h file and that should work.
My guess is that your maven file is misconfigured and hence gcc compiler cannot find your source file LegacyLibrary.h.
Configure your pom.xml using the following link as a reference.
https://github.com/oltzen/JavaCppExample/blob/master/pom.xml
Pay attention to line 53 and 65. Fill in the correct package names. This helps the compiler find out where your LegacyLibrary.h is.
Also, watch this video clip https://www.youtube.com/watch?v=LZrrqZLhtmw which walks you through the whole process how to run Javacpp with maven and eclipse.
I am on a Mac running Netbeans 6.9.
I downloaded and installed LWJGL using this tutorial down to the letter:
http://lwjgl.org/wiki/index.php?title=Setting_Up_LWJGL_with_NetBeans
I finished the installation and copied sample code to see if my system is working. I got a bug, and was not sure if it was because of faulty code or I was doing something wrong. So I shortened down the code to this little simple bit:
package javaopengl;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
//Testing
public class Main {
public static void main(String[] args) {
boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
try {
Display.create();
Display.destroy();
} catch (Exception e) {
e.printStackTrace(System.err);
}
System.exit(0);
}
}
But I still get the same error:
run:
Exception in thread "main" java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I am not sure what exactly is going on, Would you please tell me what is going on and how to fix it?
Note: When i am looking at the text in the development environment, it does not show those red lines indicating there are any errors.
What are you typing (or what is netbeans running) to run this? Since the Mac filesystem is fairly case-agnostic unless you've specified otherwise, running java javaopengl.main will look for a file main.java, which is there (Main.java will be returned). But the class is Main, and you can get this exception from the difference. If this is being run from an ant script, I suggest making sure you have the correct capitalization (the class should be javaopengl.Main). A simple way to test this is to delete everything except the class definition and an empty public static void main(String[] args) {}
Alternatively, you could have something simpler, like your classpath out of whack. Missing the lwjgl jar would get you there, but if you followed the directions in that tutorial, that actually seems less likely. Still, you can test this.
package javaopengl;
public class Main {
public static void main(String[] args) {
System.out.println("well, main works");
Class checkjar = Class.forName("org.lwjgl.opengl.Display");
System.out.println("My ClassLoader found: " + checkjar.getCanonicalName());
}
}
Also, remove import org.lwjgl.Sys; from your shortened example. It doesn't appear to be needed, provided it isn't the source of your problems :).
I have a compiled java class:
Echo.class
public class Echo {
public static void main (String arg) {
System.out.println(arg);
}
}
I cd to the directory and enter: java Echo "hello"
I get this error:
C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
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)
Could not find the main class: Echo. Program will exit.
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
Try:
java -cp . Echo "hello"
Assuming that you compiled with:
javac Echo.java
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
Echo.java
Echo.class
Then any of this may work:
java -cp . Echo "hello"
or
SET CLASSPATH=%CLASSPATH;.
java Echo "hello"
And later as Fredrik points out you'll get another error message like.
Exception in thread "main" java.lang.NoSuchMethodError: main
When that happens, go and read his answer :)
With Java 11 you won't have to go through this rigmarole anymore!
Instead, you can do this:
> java MyApp.java
You don't have to compile beforehand, as it's all done in one step.
You can get the Java 11 JDK here: JDK 11 GA Release
You need to specify the classpath. This should do it:
java -cp . Echo "hello"
This tells java to use . (the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is my.package.Echo and the .class file is bin/my/package/Echo.class, the correct classpath directory is bin.
You have no valid main method... The signature should be:
public static void main(String[] args);
Hence, in your case the code should look like this:
public class Echo {
public static void main (String[] arg) {
System.out.println(arg[0]);
}
}
Edit: Please note that Oscar is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.
If you have in your java source
package mypackage;
and your class is hello.java
with
public class hello {
and in that hello.java you have
public static void main(String[] args) {
Then
(after compilation)
changeDir (cd) to the directory where your hello.class is.
Then
java -cp . mypackage.hello
Mind the current directory and the package name before the class name.
It works for my on linux mint and i hope on the other os's also
Thanks Stack overflow for a wealth of info.
My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were
S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class
My code contained the following line
System.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());
First, I had to move to the classpath directory
cd /D "S:\Accessibility\tools\src\test\java"
Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.
set classpath=%classpath%;.;..\..\..\src\main\resources;
Then, I had to run java using the classname.
java com.accessibility.HelloWorld
First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:
public static void main(String[] args){
Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:
System.out.println(args[0])
If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.
for(int i = 0; i < args.length; i++){
System.out.print(args[i]);
}
System.out.println();