I have some problems testing a program from a java programming book. I wrote the code in eclipse and succeded running it, but when I compile the .java file via the command line and then run it with java ConsoleEcho, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: ConsoleEcho (wrong na
me: einausgabe/ConsoleEcho)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The code for my program is:
package einausgabe;
import java.io.IOException;
import static java.lang.System.*;
public class ConsoleEcho {
public static void main( String args[] ) throws IOException {
while( true ) {
int code = in.read();
out.write(code);
}
}
}
The commands I used are:
javac ConsoleEcho
java ConsoleEcho
I run this from the directory ..\Java\ProgrammierenMitJava2\src\einausgabe
I don't get any errors by compiling. Tried java einausgabe.ConsoleEcho and then I get this error message:
Error: Could not find or load main class einausgabe.ConsoleEcho
You need to include the package name in the command line, something like:
java einausgabe.ConsoleEcho
you appear to just have the class name in your command.
It seems that you forgot the package name
Use
java einausgabe.ConsoleEcho
Related
I'm trying to detect my webcam using com.github.sarxos library. This is the download link to the library.
import com.github.sarxos.webcam.Webcam;
public class DetectWebcamExample {
public static void main(String[] args) {
Webcam webcam = Webcam.getDefault();
if (webcam != null) {
System.out.println("Webcam: " + webcam.getName());
} else {
System.out.println("No webcam detected");
}
}
}
But every time I run the program it throws an Exception like this.
Exception in thread "main" java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at webcam.WebCam.main(WebCam.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 2 more
Java Result: 1
Can you please tell me what is the wrong with the Code or Library?
I have added the Library (Which I downloaded in the link given above as a .zip file) to the Library folder in Netbeans. Are there any libraries that I should Add???
You didn't add the third-party dependencies of the library, in the dl link there should be libs file containing (slf4j-api-1.7.2.jar, bridj-0.6.2.jar), add those .jars into your project and it should work. The exception is simply saying it cannot find the classes, that is needed to run the library as they are contained different .jar file
I saw this post but I wasn't clear on what the resolution was.
I'm trying to run a java program from command line utilizing an external library. Here is the total output of my session:
grifter#host:~/java$ cat ExtractTest.java
import java.io.IOException;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
public class ExtractTest {
public static void main(String[] args) throws IOException {
System.out.println("About to open PDF");
File fh = new File("/home/grifter/test.pdf");
PDDocument d = PDDocument.load(fh);
System.out.println("Here");
PDFTextStripper ts = new PDFTextStripper();
System.out.println(ts.getText(d));
}
}
grifter#host:~/java$ javac -cp .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest.java
grifter#host:~/java$ java -classpath .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest
About to open PDF
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.pdfparser.BaseParser.<clinit>(BaseParser.java:68)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1219)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1187)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1158)
at ExtractTest.main(ExtractTest.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
... 5 more
Could someone please point me in the right direction?
Any help is appreciated.
pdf box uses dependencies, which also needs to be added to your class path. You can read about the dependecies here
The jar commons-logging and the dependencies is missing in your classpath.
You can download it here
I created a List.java file in folder UtilityPack which contains this code
package Utilities;
public class List
{
private class node{}
public void insert(int data){}
public void print(){}
public static void main(String[] s){}
}
To compile i did
C:\UtilityPack>javac List.java
But when I try to run with
C:\UtilityPack>java -classpath . List
OR
C:\UtilityPack>java List
I get error
Exception in thread "main" java.lang.NoClassDefFoundError: List (wrong name: Uti
lities/List)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I have been trying to execute this program from last 3 hours but nothing worked..please help
You need the fully qualified name e.g.
java -cp . Utilities.List
i.e. you're telling the JVM to look from the current direct (-cp .) for a class Utilities.List, which it will expect in the file Utilities\List.class.
To be more consistent you should put the .java file under a Utilities directory (yes - this is tautologous - the package specifies this, but it's consistent practise).
I would also avoid calling your class List. At some stage you're going to import a java.util.List and it'll all get very confusing!
Finally, as soon as you get more than a couple of classes, investigate ant or another build tool, and separate your source and target directories.
Use the complete name of the class to lauch your program :
java Utilities.List
But the folder name should also match the package name.
Your directory structure needs to follow your Java package pathing. IOW, if the class Listis in the package Utilities, you need to situate it in a directory called Utilities, which should be at the root level of your project, i.e. the path of the source file should be C:\UtilityPack\Utilities\List.java. When you are in C:\UtilityPack (project root), you compile and run List by referencing it as Utilities.List.
You might also consider using Eclipse, it will prevent this sort of things from happening, or any other Java IDE.
I'm trying to use Lucene StandardTokenizer as in the code below:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
public class App {
public static void main(String[] args) throws Exception {
// line 28 goes below
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
// the rest is irrelevant
}
}
It is throwing VerifyError:
Exception in thread "main" java.lang.VerifyError: class org.apache.lucene.analysis.standard.StandardTokenizer overrides final method setReader.(Ljava/io/Reader;)V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at lt.App.main(App.java:28)
Any ideas how to fix this? Thanks a lot.
Do you have 2 different versions of lucene in your classpath?
The method setReader(...) was added to StandardTokenizer in the 4.0.0-BETA version. But it was later removed from StandardTokenizer and made final in Tokenizer before the 4.0.0 release version. So I you may have the 4.0.0-BETA version of the lucene-analyzers-common jar in your classpath and the 4.0.0 release version of lucene-core jar.
LUCENE-4343 is probably the issues that changed this.
I am trying to get a basic JUnit 4 setup working using java files along with the corresponding command line tools (before I move on to eclipse - which I suspect will be easier to get working but I still want to do it this way first) and what follows is self-explanatory input/output that I think will bring out the issue(s):
deniz#debian:~$ cd /tmp/temp2/src/com/example/example
deniz#debian:/tmp/temp2/src/com/example/example$ echo $CLASSPATH
.:/home/deniz/CLASSPATH_DIR:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar:/usr/share/java/junit4.jar
deniz#debian:/tmp/temp2/src/com/example/example$ ls -l /usr/share/java/junit4.jar
lrwxrwxrwx 1 root root 16 Feb 8 2011 /usr/share/java/junit4.jar -> junit4-4.8.2.jar
deniz#debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.java MathUtilsTest.java
deniz#debian:/tmp/temp2/src/com/example/example$ javac ./*.java
deniz#debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.class MathUtils.java MathUtilsTest.class MathUtilsTest.java
deniz#debian:/tmp/temp2/src/com/example/example$ java MathUtils
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtils (wrong name: com/example/example/MathUtils)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtils. Program will exit.
deniz#debian:/tmp/temp2/src/com/example/example$ java MathUtilsTest
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtilsTest (wrong name: com/example/example/MathUtilsTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtilsTest. Program will exit.
deniz#debian:/tmp/temp2/src/com/example/example$ cat MathUtils.java
package com.example.example;
public class MathUtils
{
public static double multiply(double a, double b)
{
return a * b;
}
public static void main(String[] args)
{
double num = multiply(4.0,-5.0);
System.out.println("The number is: " + num);
}
}
deniz#debian:/tmp/temp2/src/com/example/example$ cat MathUtilsTest.java
package com.example.example;
import org.junit.Test;
import static org.junit.Assert.*;
public class MathUtilsTest
{
#Test
public void testMultiply()
{
double a = 5.0;
double b = -4.0;
double expected = -20.0;
double result = MathUtils.multiply(a, b);
assertEquals(expected, result, 0.00001);
}
}
deniz#debian:/tmp/temp2/src/com/example/example$
Could someone please tell me what's going on and how to solve this? Even the MathUtils class doesn't work and it seems well done to me; it has the package defined and has a main method.
If more information than what I provided is needed, just ask.
I apologize if I made any stupid mistakes and/or assumptions because I am extremely tired as I type this; please correct me nonetheless.
Any help would be greatly appreciated!
Thanks in advance!
Your MathUtilsTest has a package declaration:
package com.example.example;
For various reasons, this means that your physical file must be in com/example/example/MathsUtilsTest. So you need to run java from /tmp/temp2/src/.
java should be able to find the classes correctly then.
It's a good idea to always run javac from that directory as well.
First try: Use the complete class name (with package)
java com.example.example.MathUtils
Second try: Add the classpath
java -cp . com.example.example.MathUtils
And mind the directory structure
To run the tests you need to follow the instructions in Running tests