Eclipse: How do I add the javax.servlet package to a project? [duplicate] - eclipse

This question already has answers here:
How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
(16 answers)
Closed 7 years ago.
I'm using Eclipse 3.6 Helios (for Java Developers) and want to add the javax.servlet package to one of my projects.
What do I need to configure/download?

To expound on darioo's answer with a concrete example. Tomcat 7 installed using homebrew on OS X, using Eclipse:
Right click your project folder, select Properties at the bottom of the context menu.
Select "Java Build Path"
Click Libraries" tab
Click "Add Library..." button on right (about halfway down)
Select "Server Runtime" click "Next"
Select your Tomcat version from the list
Click Finish
What? No Tomcat version is listed even though you have it installed via homebrew??
Switch to the Java EE perspective (top right)
In the "Window" menu select "Show View" -> "Servers"
In the Servers tab (typically at bottom) right click and select "New > Server"
Add the path to the homebrew tomcat installation in the dialog/wizard (something like: /usr/local/Cellar/tomcat/7.0.14/libexec)

Right click on your project -> properties -> build path. Add to your build path jar file(s) that have the javax.servlet implemenation. Ite depends on your servlet container or application server what file(s) you need to include, so search for that information.

When you define a server in server view, then it will create you a server runtime library with server libs (including servlet api), that can be assigned to your project. However, then everybody that uses your project, need to create the same type of runtime in his/her eclipse workspace even for compiling.
If you directly download the servlet api jar, than it could lead to problems, since it will be included into the artifacts of your projects, but will be also present in servlet container.
In Maven it is much nicer, since you can define the servlet api interfaces as a "provided" dependency, that means it is present in the "to be production" environment.

Go to
JBoss\jboss-eap-6.1\modules\system\layers\base\javax\servlet\api\main
include JAR
jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar
For me it worked

For me doesnt put jars to lib directory and set to Build path enought.
The right thing was add it to Deployment Assembly.
Original asnwer

Download the file from http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadjavaxservletjar.htm
Make a folder ("lib") inside the project folder and move that jar file to there.
In Eclipse, right click on project > BuildPath > Configure BuildPath > Libraries > Add External Jar
Thats all

Related

Eclipse - Make dynamic web application include other projects in workspace

I'd like the exported .war file to include the other (referenced) projects in the workspace.
When I reference them in the Build Path only, it shows no "Problems", but ClassNotFoundException is thrown later, when I try to "Run on Server" or export.
Using eclipse Kepler, tomcat 7.0.42.
I've seen the solution here: Not able to configure run path for web application project in Eclipse workspace; that's how it works now. Is there a more convenient way?
Are you running Tomcat within Eclipse? Try this:
Run -> Run Configurations... (or Debug Configurations...).
Find Tomcat v7.0 Server at localhost (or whatever it's named for you) under Apache Tomcat on the left-hand side.
Go to the Classpath tab on the right.
Select User Entries and click Add Projects....
Select the projects you wish to include.
That should place that project on Tomcat's classpath.

Tools jar does not exist Eclipse JAVA

The archive: C:/Java/jdk1.6.0_26-32/lib/tools.jar which is referenced by the classpath, does not exist.
This is the error I have when run Tomcat. There was no problem cleaning or publishing - only when i start the Tomcat server.
Now, I checked my folders and there is a tools.jar in file path C:\Java\jdk1.6.0_34\lib
What am i missing?
using jre6 and have jdk1.6.0_34 installed
I found this question trying to build my project using Ant in Eclipse, and I would get the same error message as above. The suggestions in #Parth's and other answers to edit the Installed JREs is the first step. What I also needed to run Ant successfully was
Right click on build.xml, select "Run As", select "Ant Build..." (note the ...)
Click on the JRE tab
Change the Runtime JRE to the same thing that you updated in the first stage, and press Apply. (That is, the runtime JRE as you specified by "Preferences" -> "Java" -> "Installed JREs", as in #Parth's answer.)
Otherwise it seems that Ant will still try to build with the old JRE that doesn't have tools.jar. Hope this helps someone.
Right click on your eclipse project, In Preferences go to Java > Installed JREs, select your JDK and click on Edit. In the new Dialog you can add external jars like tools.jar to your execution environment.
I found the solution to resolve this.
Go to Run Configurations > classpath and under User Enteries you will have tools.jar and its path would be pointing to the old folder. Remove that and click on "Add External Jars" to add the tools.jar from the new path. And that is it.
This worked for me on Eclipse:
Windows -> Preferences -> Select Java -> Select Installed JREs -> Add
-> Add your JDK -> Establish it as default -> Apply -> OK.
After this, I've to establish project related settings too:
Right click on your Project Name (in Project explorer ) -> Properties ->
Java Build Path -> Click on JRE System Libraries -> Edit ->
Select Workspace Default JRE (jdk included above)
Check your "run configurations" (Eclipse Menu Run -> Run Configurations ....) also, since therein the classpath may be wrong or has gotten obsolete.
Tomcat needs a full JDK/SDK to run but under C:/Java/jdk1.6.0_26-32/, you only have a JRE installed.
Cleaning and deploying is done by Eclipse, no Tomcat code is involved so this works.
You need to configure your environment so that Tomcat uses C:\Java\jdk1.6.0_34 as Java VM. Check the values of JAVA_HOME and the content of setenv.bat
Right click on project->Properties->Compiler->Configure workspace settings->go to configure->installed jre->give jdk path
In my case with using ANT build I had to change the setting
Add tools.jar in the below location.
Windows --> Preferences --> Ant --> Runtime --> Global Entries.
In my case the Tomcat server in Eclipse has been created under older JDK (just a few builds). My solution was to remove the sever and add it again (now using current JDK).
I had the same problem (I can reproduce it) and my solution sounds like the integration in Eclipse is really screwed up but I actually fixed it by starting Eclipse with the same JDK version.
Add/edit the following in your eclipse.ini (which is in the Eclipse directory):
-vm
path/to/jdk/bin
Where path/to/jdk/bin is the path to the JDK bin directory of the version you are using for ant/tomcat.
Note that tools.jar is NOT included in a JRE distribution.
If you are running Ant build file and getting this error in eclipse, then your eclipse is configured to use two differently named JRE's. i.e. One from the jre folder and another jre from inside the jdk's folder. Adding tools.jar may be a workaround but I used the JRE inside the JDK's folder(C:\Program Files\Java\jdk1.8.0_121). Inorder to change this,
Check in Installed JRE section which JRE is cheked(I had jdk1.8.0_121 checked and jre1.8.0_121 left unchecked).
Update your eclipse classpath by navigating to Window > Preferences > Java > Installed JRE > Execution Environments.
Now select the JavaSE-1.x on the left side and select the compatible JRE[perfect match]. Select the same JRE(jdk1.8.0_121) you had it checked above and click Ok. Optionally you can delete the JRE(jre1.8.0_121) which is unused to avoid confusion if it is not used in any other projects.
Now your project will build and you can run the Ant build files. Check the image below.
Try to add the tools.jar as an external JAR.
For eclipse,
Right click the project name and select Properties
Go to Java "Build Path"
Select tab "Libraries"
Click "Add External JARs"
Select the relevant .jar file from the file selection
I am not sure it applies to all situations, but if you right click Ant script and there is an item "External tools configuration" under "Run as" menu, select it, and go to JRE tab. As far as I can say, it may be configured independently from your project and point to the JRE, not JDK, in which case there would be missing tools.jar
I remove server (Tomcat 6.0) from window -> preferences -> server -> Runtime environment.
And add again. Then, I add my project to server again. The problem is destroyed.
If You are getting this error while running the ANT build then follow the below steps.
In the Eclipse go-to window-> preferences-> ANT-> RUNTIME
then select classpath tab then select Global Entries and add tools.jar as an external jar.
Run the ANT build.

Maven dynamic web project in eclipse deploys invalid jar files for dependent project(s)

I have a maven web project that imported into eclipse. I have another maven project (generates a jar file) that the web project depends on.
Both of these projects work correctly when executed from the command line. mvn package creates a war file which pulls the jar it depends on from the maven repository for the web project. mvn package creates a valid jar file when run for the library.
When I import the library into Eclipse, m2e recognizes that the web project depends on the library and updates parts of the project to take advantage of the fact that both maven projects are hosted within a single Eclipse workspace. The "Maven Dependencies" section of the "Libraries" tab of the "Java Build Path" preference for the web project removes the library jar file and replaces it with the library project. As the library is updated in Eclipse, the web project recognizes those changes without having to install/deploy the library.
The problem occurs when I attempt to run the web application in Eclipse. Because Eclipse is actually building the Web project against the version of the library in Eclipse, it does not deploy the library file from the maven repository, it creates a new jar file that's a snapshot of the current version of the library in Eclipse. All of this has generally worked well for me, but on the project I'm currently working on there's a problem with the jar file that Eclipse is creating. Instead of inserting the class files for the library, it appears that it's taking the "src" directory of my Maven project and zipping it up as the jar file. The contents of the jar file look something like:
-main
-java
-com
-... *.java
-resources
-...
-test
-java
-com
-... *.java
Where I would expect it to look more like:
-com
-... *.class
Since I have successfully used this type of project before, I'm trying to figure out if there's something I need to configure or if I've just run into a bug.
I'm using the latest update of m2e (1.0.200.20111228-1245) on Eclipse Indigo (Build id: 20120216-1857).
In Eclipse, we can disable the "Workspace Resolution" so that it will use our dependencies directly from the local maven repository as the following steps: -
Disable Workspace Resolution
Right click at the project inside the eclipse
Select "Maven" from the context menu.
If it display "Disable Workspace Resolution" which means it is enable. We click it with purpose to disable it. Please note, after that it will display "Enable Workspace Resolution" which means we have disabled already.
We may need to update the project configuration and other related dependencies as the following steps: -
Update Project Configuration
Right click at the project inside the eclipse
Select "Maven" from the context menu.
Click the "Update Project Configuration..."
The "Update Maven Dependencies" windows will be displayed.
Select the required projects and click "OK".
Update Dependencies
Right click at the project inside the eclipse
Select "Maven" from the context menu.
Click the "*Update Dependencies"
The "Update Maven Dependencies" windows will be displayed.
Select the required projects and click "OK".
Please take a note, since we not use the related dependencies directly from the workspace anymore, It will use directly from our local maven repository. Then all related artifacts should be installed to our local maven repository by using the following command line.
mvn install
Anyhow it can be done by using the context menu inside the Eclipse as the following steps:-
Right click at the project inside the eclipse
Select "Run As" ---> "Maven Install".
Here's the solution that I came up with.
It appears that in my case the rules for the "Deployment Assembly" for the library project are still followed.
To change it go to the library properties->Deployment Assembly
In my case that just contained a mapping from '/src' -> '/'. I removed that entry and replaced it with a mapping from '/target/classes' -> '/' and '/src/main/resources' -> '/'.
Once I had that mapping in place, when the jar file was deployed it contained exactly what I had configured in the Deployment Assembly. That allows Eclipse to continue to be used without explicitly republishing the library for every change. But, it doesn't seem to auto redeploy to tomcat for me when the library is updated -- I still need to manually restart tomcat for library changes to be reflected.
It's still not entirely clear to me what's going on with this project. I do have previous projects that have a similar structure, and in those projects the Deployment Assembly has not been updated to explicitly include the class files and yet the proper jar file is still deployed.

How to change Liferay Portlet Plugin API library in Eclipse?

I have switched my web-application to run on another installtion of Liferay. Consequently, I was able to changle classpath libraries in my project to new server location. This includes JRE System Library and Liferay v6.0 CE. I.e, both these libraries can be added for new server location from Java Build Path dialog.
But the library "Liferay Portlet Plugin API" still refers old Liferay installation path and I see no way to change it.
How is it possible?
UPDATE 1
Here: http://www.liferay.com/community/wiki/-/wiki/Main/Liferay+IDE+Getting+Started+Tutorial
On the very most low picture you can see all three libraries listed in Package Explorer. Two of them I was able to change and the question is about third one.
You can manually change file .metadata\.plugins\org.eclipse.jdt.core\variablesAndContainers.dat.
So open your filesystem browswer (My computer) navigate to your liferay IDE workspace location and go to .metadata\.plugins\org.eclipse.jdt.core. Open file variablesAndContainers.dat and find and edit your jars locations.
After that restart Liferay IDE or simply close and open your project.
UPDATE:
1) Take note of jars that are needed
2) Go to configure build path dialogue than go to libraries and select "Liferay Portlet Plugin API" then click Remove
3) Create folder "lib" directly in your project (not WEB-INF/lib)
4) Add jars to lib folder (from 1.)
5) Select jars and add them to build path (right click - Build path - add to build path)
Or you can crete you User libarary with those jars and than add it to build path
Additionally to chosing correct libraries in classpath dialog, one should also change "target runtime" appropriatedly by right click project and going to properties > Targeted Runtimes.
P.S. And plus one should close project and then open it again.
in my case, following instructions in this question about clearing eclipse cache (-clear option) helped, as changing liferay plugins SDK path wouldn't change the libraries' locations when adding this library to project build path...

How can I add my application to Tomcat in Eclipse?

I have a web app built with Maven.
Most of the time, I built the application using Intellij IDEA, but now I want to import the application in Eclipse to see something in this IDE.
After mvn eclipse:eclipse, I add in Eclipse the app, but when I try to add the app to the server (tomcat), I receive:
There are no resources that can be added or removed from the server
Solutions?
You need the project to have a Eclipse Dynamic Web Module facet. You can try doing this via the project properties dialog and looking at project facets, then clicking the appropriate check box. This may not be available however, so you may need to do the next thing.
Create a backup of your project and follow the directions at dzone.com. This'll allow you to modify the project facets via your eclipse .project file.
Keep in mind, once you add those facets, you cannot change them back via Eclipse. Definitely make sure you have a backup before starting.
This step worked for me:
Right-Click on the Project Name, then
Maven-->Update Project..
Click OK
After doing this I was able to see my project in Add/Remove Programs.
As mentioned somewhere else adding Eclipse WTP didn't do any good, however I performed the above step after I had already added Eclipse WTP, so I am guessing that may not be needed.
This was done for Eclipse Juno.
Hope this helps someone.
Edited: You can follow this link as above mentioned I think might have needed WTP and that add a lot of other things into the workspace.
http://blog.teamextension.com/maven-as-eclipse-dynamic-web-module-556
Check whether Maven Integration for Eclipse WTP is installed. If not, install it from Eclipse Marketplace
After installation restart Eclipse
Right click your project and Maven--> update project configuration
Delete tomcat and re create tomcat (clean tomcat,add your project,publish and enjoy)
I've got Tomcat 6, Java 1.6, and was trying to get it to work in Eclipse Juno Service Release 1's "internal server" (whatever that's called.) Here's what I did that worked for me:
(Found these instructions at http://www.mkyong.com/eclipse/eclipse-ide-tomcat-version-6-0-only-supports-j2ee-1-2-1-3-1-4-and-java-ee-5-web-modules/)
I should point out that I followed ClutchDude's instructions to make my project a Dynamic Web Module facet. It didn't work on its own for me, but maybe it was part of making it work in the end.
Using Eclipse (or other text editor), open the file
.settings/org.eclipse.wst.common.project.facet.core.xml
Find the line that reads
<installed facet="jst.web" version="3.0"/>
3.0 is for Tomcat 7.x (so says mkyong.) Change it to 2.5 (or 2.4).
I restarted Eclipse and it worked for my project.
The reason why this is happening is because eclipse by default looks for the deployment descriptor in the folder {project.home}/Webcontent/WEB-INF/web.xml where as the Maven puts them in src/main/webapp/WEB-INF/web.xml.
So when you are trying to add your project onto the server eclipse cannot find a deployment descriptor and hence you get the annoying message.
This can be solved by adding the following entries in .settings/org.eclipse.wst.common which can be found in
Project home folder in your workspace if it is a new maven project.
The folder where you already imported from if you are using Import -> Maven projects.
Make the following changes in to the org.eclipse.wst.common
wb-resource deploy-path="/" source-path="/src/main/webapp"/. Add this entry.
Edit --> property name="java-output-path"
Please check the following link from more details. http://java.dzone.com/articles/webapps-eclipse-and-maven.
The comments section has an excellent example with a working project.
This issue was fixed for me my installing additional eclipse modules. in particular, the ones related to m2e-wtp. just do a search for m2e in the plugins search listing for your version of eclipse
If you face an error like "There are no resources that can be added or removed from the server",
then
1) Go to the META_INF of your websvcsEAR and go to the application.xml.
2) Goto modules-> clickDetails and add the module that is existing. i.e., some EAR which your project demands.
3) Click ok
4) Now go to your server -> right click -> add and remove -> add your EAR.
Maybe you don't need add your application to the server's configuration. I think that you must use m2e eclipse plugin in order to launch the app.
For example, in your eclipse you will see a contextual menu called "Run as" if you right-click on your pom file. You can clicking on "Maven build" and Maven will download all the information needed to start tomcat.
I hope this helps you.
I used
mvn eclipse:eclipse -Dwtpversion=2.0
in command line in the folder where I had my pom.xml. Then I refreshed the project in eclipse IDE. After that I was able to add my project.
I was able to resolve this by removing my EAR project from my Eclipse workspace, then re-importing it.
Well, with a Spring and maven project, i got it that way:
Eclipse Version: 2019-06 (4.12.0)
Java 1.8
Tomcat 7
Yes, go to Project Properties >> Facets >> select Dynamic Web Module but dont apply yet!!!
Right under, it should appear a link Further Configuration Available. Click on it, otherwise one will have to edit .settings/org.eclipse.wst.common.component manually. Update source directory to src/main/java. (source)
Then in Facets window check Java - mine was pointing to 11 even though i don't have Java 11 installed or configured like a JRE runtime. I could only add/remove when changed to 1.8. (source). Note: That was really the problem, why don't Eclipse get this configuration automatically from the project?
Finally Eclipse creates some directories like WEB-INF and META-INF, just delete them.