Class seems to be in Classpath, but still NoClassDefFound error (eclipse) - java

I am getting the java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException error, but the class does seem to be in the classpath.
Since it expects XMLParseException to be in net/n3, I have added the net/n3/... folder instead of just the XMLParser because I assumed it would be looking for that hierarchy. (Although I tried it both ways, and neither worked.)
Here is the 'Referenced Libraries' sidebar:
And here is my Run Config:
Thanks for any help.

At runtime, Java will look for the class in net/n3/nanoxml/XMLParseException.class relative to the entries in the Java Build Path, whether that's a jar file or directory. So, the "net" folder would need to be underneath the entry in the Build Path, not itself be an entry in the Build Path.

It seems there are space in the path(Programming Projects should be ProgrammingProjects), and some times space will create issues. Please removing the spaces in path and give it a try!

Related

Java getResource is in wrong path

I hope someone can help me here, becouse I'm fighting with a problem for some time. In my main class I use this command:
System.out.println(getClass().getClassLoader().getResource("org"));
The problem I've got is that it returns:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main/org
instead of:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main/org
The problem is that it goes into classes directory instead of resources dir. As a result I can't have access to my .fxml files I need. I'm using gradle for build and currently working with JavaFX. I've tried something like:
System.out.println(getClass().getClassLoader().getResource("/resources/java/main/org"));
But I just got null :(
Do you know any method to force him to use absolute path or to look for resources in resource filder or even use something like to use "../" from linux to go up. I dodn;t find any of this
The root of your resources tree is defined by the classloader (as described in the JavaDoc). You can define the root by explicitely setting it in your classpath or preferably by using a build tool like maven and following the conventions set and used by the tool. For maven projects the root would usually be at main/java/resources.
getResource will always return the first match in the class path. So if you specify E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main before E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main in your classpath, you will get what you want.
That said, the resources are usually meant to be copied with the classes, and sometimes both are packed in a jar file, so you shouldn't worry about it.
With JavaFX use FXMLLoader;
FXMLLoader.load(new URL(getClass().getResource("/fxml/myfxml.fxml").toExternalForm()));
Make sure to pass the platform appropriate separator and use a relative path.

Ressources can't be found in maven build

I wanted to migrate my local prototype to a maven build and start getting productive. But unfortunately when calling
loader.setLocation(getClass().getResource("views/TaskWindow.fxml"));
On runtime it seems that maven doenst find the ressource xmls.
I tryed "TaksWindow.xml", "../views/TaksWindow.xml" "views/TaskWindow.xml" and "/views/TaksWindow.xml" but somehow it always gives me "no location set" error.
My project structure looks like this:
Any ideas why?
The path you specified is a relative path. Which means the classloader will look for a views folder at the location of the class (i. e. its package). You propably want to use an absolute path.
Try the following code, notice the leading /.
getClass().getResource("/views/TaskWindow.fxml")
For more information, see this answer.

Error while load-time compiling

I created an Aspectj project and added .aj files and java files. While compiling(Load-time) it shows the error
"Error: Could not find or load main class javaagent:path/aspectjweaver-1.8.2.jar"
I compiled it in eclipse by creating Load time configuration file.
Can any one tell me why
Very simple: Probably you should provide a real file system path instead of the place holder path. Also be sure to use -javaagent:... (note the dash character) instead of something like javaagent:....

Getting 'Source not found' for class in Eclipse debug

My top level debug class as specified in the debug configuration is MyDebugClass. Further down in the main, it attempts to launch an instance of class ProblemClass that is in the same package as MyDebugClass but it errors out on the constructor invocation line saying "Source not found". The project compiles fine and the .class file is indeed in the target directory, the same place as the .class for MyDebugClass.
I thought there may have been something odd with the classpath so that the same package where the debug main is not included so I made a test class in the same directory and tried to invoke an instance of it and that worked. So that is not the problem.
I also added the following code in MyDebugClass.main to try to debug the issue:
ClassLoader loader = ProblemClass.class.getClassLoader();
Class probCls = loader.loadClass("mypackage.ProblemClass");
//here below is where it errors out
probCls.newInstance();
What could possibly be causing this kind of class loading problem? There is a whole bunch of jars in the classpath so could it be that one of them contains this same class qualifier and the class loader is trying to pick it up from there and not the target directory as specified in the debug build path? How do I find from where the class loader is attempting to load a class, e.g. file system path looking for a .class file or some jar?
Look at the file in the Eclipse "Package Explorer" view. That may give you some help. Also, you say that the .class file is in the target directory, but what about the source .java file?
Other things to check are the Java Build Path of your project properties.
I hope this helps, though you didn't really give enough detail to be able to understand what's really going on.

How to import commons-io.jar jsp/java

I am getting an exception:
Cannot find symbol: FileUploadException;
I have a piece of code which uses
FileUploadException
The library that needs importing is:
org.apache.commons.fileupload.FileUploadException
The path to my project is :
D:\Projects\website
In the project folder I have each in its folder:
Tomcat, Derby, Website
I have copied:
commons-fileupload.jar and commons-io.jar
into both:
Tomcat/lib and Website/Web-INF/lib
---------------I tried this--------------
just importing the library on its own
import org.apache.commons.fileupload.FileUploadException;
adding the jars to the class path upon build:
javac -cp .;D:Projects\website\Tomcat\lib\commons-fileupload.jar;D:\Projects\website\Tomcat\lib\commons-io.jar com/otrocol/app/*.java
adding them to the Environment variables CLASSPATH
D:Projects\website\Tomcat\lib\commons-fileupload.jar;D:\Projects\website\Tomcat\lib\commons-io.jar
I also tried adding the jars where my .java files are as #Scot Ship suggested
----mentions---
I am not using any IDE
The code contains more unrecognized symbols, but I'm trying to solve one at a time
First time using apache, tomcat, jsp.. please be gentle
Vlad, the web container will automatically look for JARs inside
/WEB-INF/lib
even without any developer intervention. Take note that it's all caps WEB-INF. As long as your JAR is there, it will be in your web application's classpath.
Try to display this in one of your servlets or JSP:
System.getProperty("java.class.path")
and you'll get a better view of what classes and JARs were actually loaded.
Update: After reviewing your question, it appears you're facing issues in compiling the files to begin with and you're doing it outside an IDE.
Take note that when you use -cp in javac like this:
javac -cp .;D:Projects\website\Tomcat\lib\commons-fileupload.jar;D:\Projects\website\Tomcat\lib\commons-io.jar com/otrocol/app/*.java
Whatever value you have set in the CLASSPATH environment variable becomes ignored.
Be absolutely sure that the class FileUploadException is indeed inside one of the JARs you're trying to import: you can view the JAR directly using an unarchiving tool.
Also, change the com/otrocol/app/*.java to com\otrocol\app*.java - you should be using your system delimiter (not that this may affect your problem).
Create a simple HelloWorld in the same location as the file you're compiling, add the SystemOut mentioned above, and compile it the same way you're doing for the concerned file.
Read this http://commons.apache.org/proper/commons-fileupload/faq.html#class-not-found. Probably you have the fileupload jar but you also need commons-io.jar in your classpath as well.

Categories

Resources