Unable to run hadoop aplication due to NoClassDefFoundError - java

I am new in Hadoop and I have been following some tutorials like this. I have found a nice set of mapreduce examples in here. I was able to run wordcount example but I am not able to run the EnhancedTopN example. It gives me the error: Exception in thread "main" java.lang.NoClassDefFoundError: EnhancedTopN (wrong name: samples/topn_enhanced/EnhancedTopN). I have correctly compiled the java file, although it gives me a note saying "Note: EnhancedTopN.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.". What could be wrong?
Thanks

NoClassDefFoundError with the message "wrong name" means that the package structure of your class is wrong, or that you are running it the wrong way. This has nothing to do with Hadoop, just with the way packages work in Java.
Is your class EnhancedTopN in a package named samples.topn_enhanced; do you have the following package statement at the top of the source file?
package samples.topn_enhanced;
public class EnhancedTopN {
// ...
}
The directory structure of your project must match the package structure, so the source file EnhancedTopN.java should be in a directory samples\topn_enhanced, and you must compile and run it from the base directory of the package structure:
C:\Project> javac samples\topn_enhanced\EnhancedTopN.java
C:\Project> java samples.topn_enhanced.EnhancedTopN
Going into the directory and using java EnhancedTopN will not work and will give you the error that you are asking about:
C:\Project\samples\topn_enhanced> java EnhancedTopN -- error!
See: Lesson: Packages in Oracle's Java Tutorials.

Related

Creating Java Packages in IntelliJ

I've been using Eclipse for a while and I'm having trouble understanding what's going on with my first project in IntelliJ. I've read the documentation, and searched other questions, but I still can't seem to grasp it. I think there is something wrong with my project structure. This is what my structure currently looks like;
I'm trying to run the JavaForLoop class, but whenever I do, compilation fails because I have errors in the StringMethods class of the strings package. My question is why would that prevent compilation if the two classes are in separate packages? Neither class uses the other, and they both have the appropriate package declaration statements. With a similar structure in Eclipse, this would work. Should I be using a different project structure?
By default IDEA adds Build Configuration which is executed before launch and includes following steps (taken from here):
Compiling source code in the source path of a module and placing results to the output path.
Compiling source code in the test path of a module and placing results to the test output path.
Creating copies of the resource files in the output path.
Reporting problems in the Messages tool window.
check if it's your case in Edit Configuration screen and if so, remove it.
To use a class from a different package you must declare a import statement to the class.
In your JavaForLoop.java add the import before the class statement (and after package declaration where its the case)
//package ...
import strings.StringMethods;
//public class JavaForLoop { and the rest of the code
Intellij uses regular javac, which will fail to compile if you have errors anywhere in the code.
Eclipse has it's own compiler, that allows to compile and even run code that has compilation errors, causing a runtime exception if any part of the code that has errors is run. This allows you to run parts of the code that work even if other pieces of code are failing.
The simple solution is to resolve your compilation errors. You can also use the eclipse compiler with Intellij, but I've never done this so I can't comment on how well it works.

How does the terminal compile java programs when $CLASS_PATH is not set? [duplicate]

I'm trying to look under the hood about java compilation. So I put my IDE away and started using MS-DOS command-line...
I created a simple project, as described in the tree below :
SampleApp
|____**src**
|_____pack
|______Sample.java
|____**classes**
This is the Sample.java source code :
public class Sample
{
private String s = new String("Hello, world");
public Sample(){
System.out.println(s);
}
}
I just want to compile this class, so I used the javac command :
prompt\SampleApp\src>javac -d ..\classes -sourcepath . pack\Sample.java
All works fine, but i didn't expect that because I deleted my CLASSPATH environment variable before compiling my Sample.java file. So I was expecting a compiler error due to the fact that the compiler would not be able to find the java.lang.String class file.
I read this article http://www.ibm.com/developerworks/java/library/j-classpath-windows/ which helped me understand many things. The article author says that the default classpath is the current working directory. But I don't understand why my source code compile without error. Could someone explain this to me?
So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.
The short answer is that the compiler knows where to find all of the standard Java SE library classes without you telling it.
The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.
The classpath variable doesn't do what you think. To cite the oracle documentation:
The CLASSPATH variable is one way to tell applications, including the
JDK tools, where to look for user classes. (Classes that are part of
the JRE, JDK platform, and extensions should be defined through other
means, such as the bootstrap class path or the extensions directory.)
Source: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
Basically since java.lang.* is part of the platform and delivered with the JDK/JRE, the compiler doesn't have to be told by you where to look for them.

I have to compile hello.scala which has a import command "org.scala._"

I know where this folder is located, how to run hello.scala from terminal
I am doing scala hello.scala and it generates an error
/Users/username/Desktop/hello/hello.scala:1: error: illegal start of definition
package org.scala
^
one error found
How can I run my program?
Include the package name when using the scala command and run it against the class file instead of the source code
scala org.scala.Hello
Like Java, Scala Naming conventions indicate that classes start with an initial uppercase letter for classes, e.g. Hello

Odd NoClassDefFoundError

After installing the latest version of Vuze (Azureus), I got an odd error trying to start it:
> java -Xmx128m -classpath ./Azureus2.jar:./swt.jar -Djava.library.path=/bt_work/vuze -Dazureus.install.path=/bt_work/vuze -Dazureus.script=./azureus -Dazureus.script.version=2 org.gudy.azureus2.ui.swt.Main
Exception in thread "main" java.lang.NoClassDefFoundError: org/gudy/azureus2/ui/swt/Main
Caused by: java.lang.ClassNotFoundException: org.gudy.azureus2.ui.swt.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
What's odd is this:
> javap -classpath ./Azureus2.jar:./swt.jar org.gudy.azureus2.ui.swt.Main
Compiled from "Main.java"
public class org.gudy.azureus2.ui.swt.Main extends java.lang.Object{
public static final java.lang.String PR_MULTI_INSTANCE;
...
So ... running javap with the same classpath finds the class but java alone can't. WTF is going on?
I checked that both programs come from the same install Java (/usr/lib64/jvm/java-1.6.0-sun), that's Java 6 and the classes were compiled for Java 5. The manifest isn't signed. The JAR file is readable (unzip -t reports no errors).
NoClassDefFoundError happens when the class itself is found but the class loader cannot load all the classes it needs.
Can you check the import headers for class org.gudy.azureus2.ui.swt.Main.java and make sure that all the imported classes can be found in your classpath. If not, add the jar files to your classpath.
Post the import section if you want me to help figure out what is still needed.
One word: AppArmor
In my case, the config didn't allow the program java to load the JARs from the new installation path.
If you have a similar problem, look into /var/log/audit.log. You should see the error messages there.
Your exception is java.lang.NoClassDefFoundError and not exactly ClassNotFoundException - so javap will still be able to disassemble the class.
As you might know NoClassDefFoundError can be seen as a linkage error. I tend to guess that the runtime is missing some required class to execute org.gudy.azureus2.ui.swt.Main
I guess it requires more JARs on classpath.
So org.gudy.azureus2.ui.swt.Main is available (that is why javap works) but one of its dependency is not found during runtime.
Also running SWT sometimes requires -Djava.library.path set to swt library (looking at your SO reputation I guess you know this)
Edit
Here is a link to one Azureus shell script, which lists more classpath JARs.

Classpath compiles correctly, but will not run. What am I missing

Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
My guess is that . is not on your classpath. For example, you might be compiling with:
javac -cp foo.jar:bar.jar Test.java
but then to run the code you'd need
java -cp foo.jar:bar.jar:. Test
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.
Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.

Categories

Resources