ClassNotFoundException from jar in Dynamic Web Project - java

I created a JPA project that I want to use in a dynamic web project.
I made a jar from this JPA project and added it in a lib folder in the dynamic webproject. I then added this to the buildpath of the dynamic web project and also ticked it in the "Order and Export" tab.
First question : I created the jar file with a normal export to jar with these options;
Are these ok or do I have to add something.
Second question: After I added this jar to the buildpath and I use a instance of a class from within this jar I can add it to the imports and the class is know so I don't see any errors.
When I try to run the jsp and servlet where I use the class I get a java.lang.NoClassDefFoundError error in my apache with a java.lang.ClassNotfoundEception at the line where I use the class. So for some reason he doesn' find the class at runtime.
I tried to make the jar over and over again with different options. I tried to import the class I need on different way's (ctrl-T) but I only see one class.
The only thing I find strange is the way the import looks. In my JPA project I have a package structure like :
be.JPA.project6
in my dynamic web project I have a package structure like :
be.project6
when I see the import, I see : be.JPA.project6.data.classname
I would expect that a different structure so he would know that he needs to search outside the "normal" package structure. But also with "ctrl-T" I only can see this class
I don't want to make a reference to the other project but use it like this with a jar.
I'm out of any clues at the moment, so any advise would be more then welcome.

The build path is that: a build path. A path used to build the application.
The runtime classpath of a webapp is composed from the WEB-INF/class directory, and the set of jars in WEB-INF/lib.
Remove the jar from the build path of the dynamic web project. The just drop it into the WebContent/WEB-INF/lib directory. Eclipse will automatically add all the jars in this directory to the build path, and will put them in the WEB-INF/lib directory of the deployed web application, so that they're also part of the runtime classpath.

Related

while loading excel data into database using servlets ( java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook) [duplicate]

How should I add JAR libraries to a WAR project in Eclipse without facing java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError?
The CLASSPATH environment variable does not seem to work. In some cases we add JAR files to the Build Path property of Eclipse project to make the code compile. We sometimes need to put JAR files inside /WEB-INF/lib folder of the Java EE web application to make the code to run on classes inside that JAR.
I do not exactly understand why CLASSPATH does not work and in which cases we should add JARs to Build Path and when exactly those JARs should be placed in /WEB-INF/lib.
The CLASSPATH environment variable is only used by the java.exe command and even then only when the command is invoked without any of the -cp, -classpath, -jar arguments. The CLASSPATH environment variable is ignored by IDEs like Eclipse, Netbeans and IDEA. See also java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable.
The Build Path is only for libraries which are required to get the project's code to compile. Manually placing JAR in /WEB-INF/lib, or setting the Deployment Assembly, or letting an external build system like Maven place the <dependency> as JAR in /WEB-INF/lib of produced WAR during the build, is only for libraries which are required to get the code to deploy and run on the target environment too. Do note that you're not supposed to create subfolders in /WEB-INF/lib. The JARs have to be placed in the root.
Some libraries are already provided by the target JEE server or servletcontainer, such as JSP, Servlet, EL, etc. So you do not need put JARs of those libraries in /WEB-INF/lib. Moreover, it would only cause classloading trouble. It's sufficient to (indirectly) specify them in Build Path only. In Eclipse, you normally do that by setting the Targeted Runtime accordingly. It will automatically end up in Build Path. You do not need to manually add them to Build Path. See also How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and JEE libraries which are not provided by the target servletcontainer (e.g. Tomcat doesn't support many JEE libraries out the box such as JSF, JSTL, CDI, JPA, EJB, etc), need to end up in /WEB-INF/lib. You can just copy and paste the physical JAR files in there. You do not necessarily need to specify it in Build Path. Only perhaps when you already have it as User Library, but you should then use Deployment assembly setting for this instead. See also ClassNotFoundException when using User Libraries in Eclipse build path.
In case you're using Maven, then you need to make absolutely sure that you mark libraries as <scope>provided</scope> if those are already provided by the target runtime, such as JEE, Servlet, EL, etc in case you deploy to WildFly, TomEE, etc. This way they won't end up in /WEB-INF/lib of produced WAR (and potentially cause conflicts with server-bundled libraries), but they will end up in Eclipse's Build Path (and get the project's code to compile). See also How to properly install and configure JSF libraries via Maven?
Those JARs in the build path are referenced for the build (compile) process only. If you export your Web Application they are not included in the final WAR (give it a try).
If you need the JARs at runtime you must place them in WEB-INF/lib or the server classpath. Placing your JARs in the server classpath does only make sense if several WARs share a common code base and have the need to access shared objects (e.g. a Singleton).
If you are using Maven:
Open the project properties, and under Deployment Assembly click Add...
Then select Java Build Path Entries and select Maven Dependencies
Resolved by setting permissions.
Had related issue using PySpark and Oracle jdbc. The error does not state that the file cannot be accessed, just that the class cannot be loaded.
So if anyone still struggles, check the permissions. Some might find it obvious tho'.
I want to give the answer for the folowing link question ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse
Ans: In Myeclipse go to Server-->left click on Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)-->ok

is Bootstrap configuration mandatory to run dynamic project in eclipse?

To run dynamic project in eclipse, I have already added required jars in project class path, some jars path are reference from other open project. even though it is throw class not found error. when I put that jars in bootstrap entries, it works fine.
So my questions are:
1) Is Bootstrap configuration mandatory to run dynamic project?
2) Why does it not find class path while running server?
3) How can I run my project without configuring Bootstrap entries?
It must not be put in the build classpath, neither under User entries, nor under Bootstrap entries.
It must be dropped inside the WebContent/WEB-INF/lib directory of your webapp project. Eclipse will detect them, automatically add them into the build path for you, and automatically include them into the WEB-INF/lib directory of the deployed webapp, since that's where the Java EE containers look for the libraries needed by a Java EE webapp.

Netbeans can't find external jar

I'm fairly new to Java and need to add an external Jar to the project in netbeans. I've created a library with that Jar using Tools --> Libraries, and added the library in the project Properties --> Libraries under compile, but it still returns "Package does not exist".
Any help?
What version of Netbeans are you using? Make sure your libraries are located outside of the project directory. Just making sure when you add the jar, you don't just add the folder containing them but the actual jar -right?
Try building the project to see if that helps.
Perhaps the library was created incorrectly. If the package structure in the JAR isn't correct, the class loader won't be able to find the .class file.
Open the JAR with WinZip and confirm the contents.

Add python module to JAR classpath in Eclipse

I have a Java project that utilizes Jython to interface with a Python module. With my configuration, the program runs fine, however, when I export the project to a JAR file, I get the following error:
Jar export finished with problems. See details for additional information.
Fat Jar Export: Could not find class-path entry for 'C:Projects/this_project/src/com/company/python/'
When browsing through the generated JAR file with an archive manager, the python module is in fact inside of the JAR, but when I check the manifest, only "." is in the classpath. I can overlook this issue by manually dropping the module into the JAR file after creation, but since the main point of this project is automation, I'd rather be able to configure Eclipse to generate properly configured JAR automatically. Any ideas?
*NOTE*I obviously cannot run the program successfully when I do this, but removing the Python source folder from the classpath in "Run Configurations..." makes the error go away.
Figured it out, had to add the source folder with the Python module in it as a class folder in the Build Path project properties. Not sure if this next part is necessary or not, but since the module is not compiled, I added the folder again as "Attached Source" after adding the class folder.
Have a look at the maven-jython-compile-plugin and its demo project at http://mavenjython.sourceforge.net/ . It allows bundling jython with dependencies into a standalone jar.

Eclipse debug-time classpath problem: How do you include a dependent project's output into a web project's runtime classpath?

So I started with a web services project (just a dynamic web project) that builds and debugs correctly from eclipse. We've pulled a chunk of common code out that we want to put into a shared library so now those classes are going into a separate jar project that the web project references.
On the web project, I did Project->Properties->Java Build Path->Projects->Add and added the jar project. And this correctly solved all the compile-time classpath problems and everything builds fine. But at runtime, when the tomcat server fires up, spring attempts to inject some of the classes contained in the jar file and I get a NoClassDefFoundError.
My .class and properties files and the contents of my META-INF directory are showing up in the ./build directory, but my WEB-INF/lib directory seems to be referenced in-place, and the jar dependency doesn't get copied in to it to show up as part of the Web App Library.
What is the magical incantation to tell eclipse that the other jar project needs to be available to tomcat at runtime? From our ant build script, we first just build the other project into WEB-INF/lib and everything works fine, but not for eclipse debugging.
I figured this out after spending some time on it. If you are in Eclipse Helios , go to properties > deployment assembly > add > project and select the dependent project you wish to add.
Java EE module dependencies would solve this problem.
You have already done the task of extracting your common classes into its own project, possibly because other projects depend on these classes. Either way, you'll have to ensure that this is a Utility project (appears under Java EE in the project wizards), and not just a plain Java project.
One that is done, you can proceed to add the Utility project to your build path (compile-time path) as you have figured out.
The additional (final) step is to establish a Java EE module dependency between your Dynamic Web project and the shared library, which causes the utility's classes to be placed in WEB-INF\lib during deployment, and even during export of the WAR. To do so, visit the dynamic web project's properties, and browse to the Java EE module dependencies. Ensure that your utility project is selected here. Redeploy/publish your application and you should be good to go.

Categories

Resources