Can somebody please explain what happens when add some jars to classpath in eclipse or intellij. Is this classpath only for this project/application/jvm?
Thanks.
I know it's for the application to be able to use classes in the jars. What I don't understand is what this classpath is. Is it the same as the classpath in the system environment variables? Is this classpath only for this project/application/jvm? Is it possible for other projects/applications to use this classpath?
I'm working on a project which is based on Grails. And I saw this line in the configuration file.
<import resources="classpath:META-INF/............xml" />
What is the classpath here?
When you add jars to classpath, your application starting with this jars in -cp(classpath) argument. And you will not get ClassNotFoundException when use classes from external jars, that you add to classpath of your project.
When you create web application, jars that you add to classpath, will copy to lib folder of installed application and load by jmv that start your web application.
you get the file name,
When you add jars to classpath, your application starting with this jars in -cp(classpath) argument. And you will not get ClassNotFoundException when use classes from external jars, that you add to classpath of your project.
When you create web application, jars that you add to classpath, will copy to lib folder of installed application and load by jmv that start your web application.
As you can see in this answer
Related
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
I'm using Eclipse 3.7 (STS) with Tomcat 7 running inside the IDE. I've created a new Dynamic Web project and added a single JSP file to the web content root folder. I can run Tomcat and access the JSP from within Eclipse with no problems.
I've added a few 3rd party JAR's to the project from User Libraries (I'm not using maven or auto dependecies managment). In the JSP I reference a class from the project's JAR file, I can compile this with no problem, but when I deploy on Tomcat the JSP throws ClassNotFoundException. Clearly, Tomcat can't find the JAR's from my library settings. I tried creating a Run As configuration for Tomcat Server and I set the classpath to match the classpath settings of the project, but I still get the same classnotfound problem.
I could get around the issue by manually copying all project JARs to the WEB-INF/lib directory so the webapp can find all dependencies, but that's absurd and I don't expect that to be the solution since it's a maintenance nightmare.
Am I missing something?
In project's properties, go to Deployment Assembly. Add there the buildpath entries as well which you've manually added as user libraries. It'll end up in /WEB-INF/lib of the deployed WAR.
You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.
Eclipse should offer you the option of generating a WAR file that includes all the dependencies: I haven't used Web Tools for a good while but one way or another all dependencies have to be in WEB-INF/lib or the class loader won't be able to find them.
I'm using Eclipse 3.7 (STS) with Tomcat 7 running inside the IDE. I've created a new Dynamic Web project and added a single JSP file to the web content root folder. I can run Tomcat and access the JSP from within Eclipse with no problems.
I've added a few 3rd party JAR's to the project from User Libraries (I'm not using maven or auto dependecies managment). In the JSP I reference a class from the project's JAR file, I can compile this with no problem, but when I deploy on Tomcat the JSP throws ClassNotFoundException. Clearly, Tomcat can't find the JAR's from my library settings. I tried creating a Run As configuration for Tomcat Server and I set the classpath to match the classpath settings of the project, but I still get the same classnotfound problem.
I could get around the issue by manually copying all project JARs to the WEB-INF/lib directory so the webapp can find all dependencies, but that's absurd and I don't expect that to be the solution since it's a maintenance nightmare.
Am I missing something?
In project's properties, go to Deployment Assembly. Add there the buildpath entries as well which you've manually added as user libraries. It'll end up in /WEB-INF/lib of the deployed WAR.
You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.
Eclipse should offer you the option of generating a WAR file that includes all the dependencies: I haven't used Web Tools for a good while but one way or another all dependencies have to be in WEB-INF/lib or the class loader won't be able to find them.
Is there any way that I can access java libraries outside of a .war file in a Jetty server?
I have tried several things, such as expanded wars, but every time I failed, as the server didn't start each time.
Currently I build a war and excluded some JARs from it, then I've placed the JARs in the lib folder in my Jetty Distribution. For some reason though, it is not looking for libraries from that folder. I am using Jetty Version 9.
How should this be properly configured to work?
The lib directory in a jetty distribution is not automatically added to the classpath of the server. The start.jar command builds a classpath depending on what modules are enabled. You can see the constructed classpath this with:
java -jar $JETTY_HOME/start.jar --list-config
There is a module that does add any jars discovered in lib/ext to the classpath, so you can just add --module=ext to the command line or run a command like
java -jar $JETTY_HOME/start.jar --add-to-startd=ext
then any jars in lib/ext will be put on the server classpath.
The next question is will they be visible to the webapp... if they are not in a java or javax or org.eclipse.jetty package, then typically they will be visible by default. If you want to add hidden classes, then you need to adjust the contexts server and system classes configuration.
I am deploying a web application in a Jetty container and I need to know how to add a classpath for Jetty to be able to load resources from a testing directory.
I think it's best to not modify the Jetty CLASSPATH; use what you know about the defaults for your web app.
All the JARs in WEB-INF/lib are your web app's CLASSPATH; so are all paths relative to WEB-INF/classes. If you put a directory /test-resources under WEB-INF/classes and load it as a resource stream from the context you'll be able to access those test files without having to alter the Jetty startup scripts.
I find it much easier to keep my test jars in a separate folder where they are built by a different process or IDE.
I extracted start.config from the Jetty jar and added my classpath to it as documented at
http://docs.codehaus.org/display/JETTY/A+look+at+the+start.jar+mechanism