Getting ClassNotFound Exception for class MkActivityMethod - java

I am trying to use JackRabbit lbrary for SVN chekin operation.
What I run my code through individual program this works fine. But when I run using web based project it doesn't work.
Code compiles fine but gives me runtime exception at following line :
MkActivityMethod activityMethod = new MkActivityMethod(url);
The exception is :
java.lang.NoClassDefFoundError: org/apache/jackrabbit/webdav/client/methods/MkActivityMethod
My project has jackrabbit-standalone-2.6.4.jar included in my eclipse jars as well as in project web-Inf/lib folder
Please let me know where I am going wrong.

There is obviously something wrong with your classpath. What Web Server Are You Using?

Here is my solution :
Try to build the project once again
Check the JDK version for the builder and Server JRE
Try clean the project (In eclipse , Project menu -> Clean )
Reason :
After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. It probably indicates that you haven't set the classpath option when executing your code. This link explains how to set the classpath when you execute.

Related

How do I resolve "the type is not accessible" error in Eclipse when using an external JAR? JAR in question is algs4 from Princeton [duplicate]

I'm new to the whole programming stuff but here's my problem:
I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.
Currently, I am not able to implement any test methods because Eclipse tells me on the line
import static org.junit.jupiter.api.Assertions.*;
the error message
The type org.junit.jupiter.api.Assertions is not accessible.
Error I get in the IDE:
I tried the following:
Reinstalling Eclipse, using a fresh workplace.
Adding the JUnit to Build path
Nothing helped.
It worked and works in older projects just fine.
Here is how the Package Explorer looks:
What am I missing?
You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

NoClassDefFoundError for IndentingXMLStreamWriter class in stax-utils.jar

I am writing an XML file with StAX parser using XmlStreamEventWriter, the stax-utils.jar is added correctly to the classpath in Eclipse. I wanted to use IndentingXMLStreamWriter class from this jar file but somehow its throwing below exception:
java.lang.NoClassDefFoundError: javanet/staxutils/IndentingXMLStreamWriter
Caused by: java.lang.ClassNotFoundException: javanet.staxutils.IndentingXMLStreamWriter
cannot be found by RCP_PLUGIN_6.20.0.qualifier
The piece of code causing exception is :
XMLEventFactory eventFactory=XMLEventFactory.newInstance();
if(!tmpSettingsXml.exists())
tmpSettingsXml.createNewFile();
XMLStreamWriter xmlStreamWriter=new IndentingXMLStreamWriter(XMLOutputFactory.newInstance().
createXMLStreamWriter(new FileOutputStream(tmpSettingsXml)));
xmlStreamWriter.writeStartDocument();
If the method containing 'IndentingXMLStreamWriter' is being called from other class of the tool its throwing exception, however, if this method is called from main() in other class its perfectly working fine and able to find classes inside jar.
Anyone can suggest what's wrong here?
An Eclipse/RCP plug-in runs within an OSGi Runtime, which (almost) completely controls its runtime classpath based on the Plug-in Manifest: the MANIFEST.MF file. You need to add any dependencies that aren't your own sources there, and not directly using the Java Build Path UI. The Java Build Path UI isn't locked out because a Plug-in Project is still a Java Project, just with more stuff.
So remove the jar(s) you added to the Java Build Path, open the MANIFEST.MF file in the Plug-in Manifest Editor, and add the jars to the Classpath section on the Runtime tab.

ClassNotFoundException/NoDefFound on Runtime, but class imported works on it's own?

I'm trying to import a jar that I created to my project in Eclipse. When trying any calls from the so-called jar, I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
I believe this is caused due to the class being present on compile, but not being executed in runtime.
The problem is: When I run the jar on its own (without being imported), it works as intended, with all the functionalities. But I now need it to function as being part of a bigger jar, without changing the runtime classpath (didn't do that to the original project).
How can that be achieved?
#Edit
The class not being found or def not being found is ByteList, from the FastUtil library.
Exception in thread "main" java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/bytes/ByteList
It's imported TO another Jar, let's call it MyJar.jar; MyJar.jar functions perfectly as a standalone jar, as the intention for it is to be a library, and it functions by itself.
The situation is: When I import MyJar.jar to another project, it won't work anymore, I can't use it's methods and classes inside the new project.
MyJar.jar can be run from the command prompt, with java -jar MyJar.jar, and it will print it's test-page to show that it's working.
The new project compiles, but when it's ran, I get the class not found exceptions regarding MyJar project.
You can use Maven Assembly Plugin to add all classes from related JARs, otherwise you cannot load classes without adding them to classpath.
If you don't want to use Maven then add all needed classes manually to your JAR.

How do you create a jar file of classes that can be re-used by multiple Android projects?

Here are the steps I have followed so far, with no luck. I am extremely new to Java projects so I suspect I may be missing something obvious.
Using Eclipse, I have created a simple Java project called TestSDK, created within that a package called com.test.testsdk, and within that the following class:
package com.test.testsdk;
public class TestClass {
public void TestMethod() {
}
}
This compiles without errors or warnings.
I then export this as a JAR file (TestSDK.jar) using Eclipse and the standard export options (export generated class files and resources, compress the contents of the JAR, generate manifest file). I have tried both sealing and not sealing the JAR which makes no difference.
I then create a new Android application project from File->New->Project in the Wizards list. This compiles and runs without warnings or errors on both the Android emulator and my test device (I get the hello world message).
I then add a reference to my TestSDK.jar file (using a variety of different methods as I will expand on shortly), import it into the main (and only) Android activity, and try to instantiate my TestClass and call TestMethod on it, like so:
package com.apptest.mobilesdktestapp;
import android.app.Activity;
import android.os.Bundle;
import com.test.testsdk.TestClass;
public class MobileSDKTestAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestClass test = new TestClass();
test.TestMethod();
}
}
This compiles fine without warnings or errors. When trying to run it on the emulator or the device, however, I get the following error in my LogCat window:
AndroidRuntime Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime java.lang.NoClassDefFoundError: com.test.testsdk.TestClass
Searching the web for the NoClassDefFoundError results in a lot of suggestions on how to import the JAR file such that the class path is correct. As a result, I have tried all of the following methods of importing the JAR file:
"Add External JARs..." from the Libraries tab of Java Build Path in the project properties, followed by checking (or not checking, I tried both) the JAR in the Order and Export tab. Also tried moving the JAR to the top of the Order and Export list, which made no difference.
Creating a "libs" folder in the project, and adding the JAR there. I confirmed that the JAR is then also added to the "Android Dependencies" thing in the project list. Also tried right-clicking the JAR file and selecting Build Path->Add to Build Path which made no difference.
Moving the JAR into my Android Application project directory and doing "Add JARs..." instead of external JARs as in step 1, also all permutations of exporting or not and moving it to the top of the order list or not.
I have subsequently downloaded other 3rd party SDKs that are packaged as JAR files and included those in the very same Android application project, and those have all worked fine using any of the 3 methods above (I am able to instantiate classes from those SDKs and use them without error), which leads me to believe I am missing something or doing something wrong in my TestSDK project and/or class which is preventing it from being used in the Android Application project.
As I said, I am very new to Java, so I'm hoping it's something simple that I've overlooked. Any help or guidance would be appreciated.
If you are on R17 or higher version of the Android tools and ADT in Eclipse, then the first sentence of #2 is the correct answer; everything else listed in your question is unnecessary at best or harmful at worst.
I would recommend that you create a clean project, create the test activity, create the libs/ folder, copy the JAR into the libs/ folder, code to the JAR's API, compile, and run. If that works, then your original project still has stuff lingering around from your previous efforts that is causing you grief. If it fails, then something fairly strange is going on. The JAR itself is presumably fine, otherwise you would get compile errors.
I think I figured out what the issue was (or at least how to fix the issue, I'm still not 100% sure what I did)--
When I created the original TestSDK java project, I let it target the default JRE in the project creation dialog (jre7).
Checking the project properties of a new Android Application project, the Java Compiler section has "Compiler compliance level" set to 1.5. So, I tried recreating my TestSDK project again, but told it to use J2SE-1.5 as the execution environment instead of the defalut jre7.
After doing this, exporting the JAR and importing it to the Android project's libs directory, I am now able to instantiate the TestSDK classes and use them just fine without the NoClassDefFoundError exception.
Best guess is that the Android application was being compiled against an older version of the JRE than my TestSDK class (which I believe was targeting JavaSE-1.7), causing the issues. Matching the two versions up has solved it.

NoClassDefFoundError on command-line with new NetBeans project

I've created a new J2SE project in NetBeans, and I can run it from the IDE, but when I try to run it using Ant on the command line, I get the following problem:
<snip>
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: IndexBuilder
[java] Java Result: 1
<snip>
Based on the snippet from project.properties below, the class should be found.
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
How do I go about fixing this?
The error you're getting means that one of the following is true:
The class IndexBuilder cannot be found on the classpath
A necessary (for class loading) dependency of IndexBuilder cannot be found on the classpath
That is, when loading the class, it's possible (even likely) that the class can be found but that some critical dependency of the class cannot be found. For example, if IndexBuilder extends another class and that base class cannot be found on the classpath, you'll get this error. Another example is if IndexBuilder uses a class in a static initializer and that class cannot be found.
Check your classpath not just for IndexBuilder but also for anything that IndexBuilder depends on.
See, for example, this discussion of NoClassDefFoundError.
When you are running it from the command line, you are actually invoking Apache Ant. The reason you are getting the ClassNotFound Exception is because ${javac.classpath} and all the other properties are not being properly populated. That is why your code runs from within the Netbeans context. Netbeans is setting those properties for you.
To answer your original question of how do you go about getting it to run from the command line, you need to either set up a properties file that defines those parameters via a property declaration:
<property file="myproject.properties"/>
Another solution is to set the properties as environment variables via a sh script. Or you can use real paths in the build script instead of properties.
See here for more details on how to invoke Ant from the command line.
Did you try setting the working directory to "build\classes" in the Project Properties -> Run tab?
At least one of the JARs/Libs referenced by your project may not be being copied to the class path of your program. Copy all of the jars/libs that your project uses to the /dist folder of your project (or wherever YourApplication.jar is), then try to run your program. If this fixes it it means your Netbeans project isn't configured quite correctly.
Are you running this on Windows or Unix. If Windows, try changing your property file to:
run.classpath=${javac.classpath};${build.classes.dir}
Please note the semicolon instead of a colon.

Categories