Related
I am trying to run a test Maven web application (*.war file) that Inserts data into a mySQL database using tomcat. In the maven project itself I have added all the neccesary dependancies to do this. However when I try and run it on the Tomcat server I get this error:
No suitable driver found for jdbc:mysql://localhost:3306/Companies
*"/Companies" is the name of my database.
I assmumed this ment that my dependancies were not being added to tomcat during deployment. Other simular questions suggests to go to "Deployement assembly" to add the maven dependancies.
However this option is only available in eclipse and am using netbeans to run my project.
My question is how would I do the same on Netbeans?
Decide where you want to have the mariadb driver. It could be embedded in the application, then simply add that dependency to your pom (it is a mariadb and mysql driver):
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.7</version>
</dependency>
If you prefer that dependency to be resolved by Tomcat, simply drop that jar file into the $CATALINA_BASE/lib directory.
The advantage is that the responsibility for upgrading mysql in connection with the driver can be delegated away from development.
I am using thymeleaf with spring boot my index.html is located in src/main/resources/template every time I want to see a change I have to stop the server, update the project, and rerun the application, this is highly ineffeciant, I just want to run and refresh, I added devtools to my dependencies and also set spring.thymeleaf.cache= false and it still does not work
Edit the starter dependencies and add the devtools option, that way it will restart everytime you make changes.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
According to https://dzone.com/articles/spring-boot-application-live-reload-hot-swap-with you should edit registry if you are using Intellij.
Open the Settings --> Build-Execution-Deployment --> Compiler
and enable the Make Project Automatically.
Then press ctrl+shift+A and search for the registry. In the registry, make the
following configuration enabled.
compiler.automake.allow.when.app.running
Restart the IDE.
One of the main challenge for the java developers is to deploy the apps and restart server when ever there is a code change.we can reload the code changes without having to restart the server. In Spring Boot this can be achieved by adding a DevTools module, just add the following dependency in your Spring Boots pom.xml and build it.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Spring Boot DevTools module does exactly what developers needed, this eliminates the process of manually deploying the changes. DevTools will auto restart the server when we have changes. Spring team they haven’t included this feature in Spring Boot’s initial version, upon several request they added this feature later.
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?
Ensure you've the right Eclipse and Server version
Ensure that you're using at least Eclipse IDE for Enterprise Java (and Web) developers (with the Enterprise). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without Enterprise), and manually installed some related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Enterprise Java one.
You also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, RedHat WildFly, Eclipse GlassFish, etc. Usually, just downloading the ZIP file and extracting it is sufficient. In case of Tomcat, do not download the EXE format, that's only for Windows based production environments. See also a.o. Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use.
A servletcontainer is a concrete implementation of the Servlet API. Also note that for example WildFly and GlassFish are more than just a servletcontainer, they also support JSF (Faces), EJB (Enterprise Beans), JPA (Persistence) and all other Jakarta EE fanciness. See also a.o. What exactly is Java EE?
Ensure that you're using the right Servlet package
The javax.* package has been renamed to jakarta.* package since Servlet API version 5.0 which is part of Jakarta EE 9 (Tomcat 10, TomEE 9, WildFly 22 Preview, GlassFish 6, Payara 6, Liberty 22, etc). So if you're targeting these server versions or newer, then you need to replace
import javax.servlet.*;
import javax.servlet.http.*;
by
import jakarta.servlet.*;
import jakarta.servlet.http.*;
in order to get it to compile, else you might risk to face this build error
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Integrate Server in Eclipse and associate it with Project
Once having installed both Eclipse for Enterprise Java and a servletcontainer on your machine, do the following steps in Eclipse:
Integrate servletcontainer in Eclipse
a. Via Servers view
Open the Servers view in the bottom box.
Rightclick there and choose New > Server.
Pick the appropriate servletcontainer make and version and walk through the wizard.
b. Or, via Eclipse preferences
Open Window > Preferences > Server > Runtime Environments.
You can Add, Edit and Remove servers here.
Associate server with project
a. In new project
Open the Project Navigator/Explorer on the left hand side.
Rightclick there and choose New > Project and then in menu Web > Dynamic Web Project.
In the wizard, set the Target Runtime to the integrated server.
b. Or, in existing project
Rightclick project and choose Properties.
In Targeted Runtimes section, select the integrated server.
Either way, Eclipse will then automatically take the servletcontainer's libraries in the build path. This way you'll be able to import and use the Servlet API.
Never carry around loose server-specific JAR files
You should in any case not have the need to fiddle around in the Build Path property of the project. You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.
In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>. You can find examples of proper pom.xml dependency declarations for Tomcat 10+, Tomcat 9-, JEE 9+ and JEE 8- in this answer: How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?
Here are some typical exceptions which you can get when you litter the /WEB-INF/lib or even /JRE/lib, /JRE/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:
java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit
java.lang.NoClassDefFoundError: javax/el/ELResolver
java.lang.NoSuchFieldError: IS_DIR
java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
org.apache.jasper.JasperException: The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
java.lang.VerifyError: (class: org/apache/jasper/runtime/JspApplicationContextImpl, method: createELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function
jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
STEP 1
Go to properties of your project ( with Alt+Enter or righ-click )
STEP 2
check on Apache Tomcat v7.0 under Targeted Runtime and it works.
Little bit difference from Hari:
Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.
Include servlet-api.jar from your server lib folder.
Do this step
Add javax.servlet dependency in pom.xml. Your problem will be resolved.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Quick Fix- This worked in Eclipse - Right Click on project -> Properties -> Java Build Path (Tab) -> Add External JARs -> locate the servlet api jar implementation (if Tomcat - its named servlet-api.jar) -> click OK. That's it !!
you can simply copy the servlet-api.jar and copy that jar files into lib folder, which is in WEB-INF.
then just clean and built your project, your errors will be solved.
**OR**
you can directly add jar files to library by using following steps.
Right click on project.
Go To Properties.
Go to Java Build Path.
Select Add Library option from tabs.
Add Jar Files
give path of your servlet-api.jar file.
Clean and build your project.
I know this is an old post. However, I observed another instance where in the project already has Tomcat added but we still get this error. Did this to resolve that:
Alt + Enter
Project Facets
On the right, next to details, is another tab "Runtimes".
The installed tomcat server will be listed there. Select it.
Save the configuration and DONE!
Hope this helps someone.
For maven projects add following dependancy :
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Reference
For gradle projects:
dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
or download javax.servlet.jar and add to your project.
From wikipedia.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<html>\n" +
"<head><title>Hello WWW</title></head>\n" +
"<body>\n" +
"<h1>Hello WWW</h1>\n" +
"</body></html>");
}
}
This, of course, works only if you have added the servlet-api.jar to Eclipse build path. Typically your application server (e.g Tomcat) will have the right jar file.
I was getting a null pointer exception during project creation related to "Dynamic Web Module".
To get the project to compile (that is, to javax.servlet to import successfully) I had to go to project's Properties, pick Project Facets in the sidebar, tick Dynamic Web Module and click Apply.
Surprisingly, this time "Dynamic Web Module" facet installed correctly, and import started to work.
In my case, when I went to the Targetted Runtimes, screen, Tomcat 7 was not listed (disabled) despite being installed.
To fix, I had to go to Preferences->Server->Runtime Environments then uninstall and reinstall Tomcat 7.
Many of us develop in Eclipse via a Maven project. If so,
you can include Tomcat dependencies in Maven via the tomcat-servlet-api and tomcat-jsp-api jars. One exists for each version of Tomcat. Usually adding these with scope provided to your POM is sufficient. This will keep your build more portable.
If you upgrade Tomcat in the future, you simply update the version of these jars as well.
This could be also the reason. i have come up with following pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
The unresolved issue was due to exclusion of spring-boot-starter-tomcat. Just remove <exclusions>...</exclusions> dependency it will ressolve issue, but make sure doing this will also exclude the embedded tomcat server.
If you need embedded tomcat server too you can add same dependency with compile scope.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
#BalusC,
I would prefer to use the exact classes that my application is going to use rather than one provided by Eclipse (when I am feeling like a paranoid developer).
Another solution would be to use Eclipse "Configure Build Path" > Libraries > Add External Jars, and add servlet api of whatever Container one chooses to use.
And follow #kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home.
However it introduces another constraint that the developer must install Weblogic on his/her local machine if weblogic is being used !
Any other cleaner solution?
Assume a simple dynamic Web Project. Ensure Tomcat is present on the project's build path.
First get the local tomcat instance downloaded/installed on your machine. Assume we installed tomcat v8.5.
Now configure the Eclipse Global Server Run time Environment
Windows > Preferences > Server > Runtime Environments > Add > Apache > Apache Tomcat v8.5 > Next > Browse Tomcat Installation directory > Next > Finish.
Now create your dynamic web project and we see the import javax.servlet cannot be resolved issue. Resolve this by following below.
Right Click On Project > Build Path > Configure Build Path > Libraries > Add Library > Server Runtime > Select your Apache Tomcat instance > Finish > Apply and Close.
All Errors will be gone.
I've been doing a P.O.C with Spring Boot.
So far it's been going really good and promising, but there's one major drawback: I'm using an embedded server (i.e., packaging the web app in a .jar), so when developing I have to rebuild the jar and restart the server every time I change the CSS, HTML or JS files. There's not hot-swap. This really slows down the UI development.
I can think of several quick fixes, such as loading static resources off a different domain and serving it from a local nginx, and some more variations like this, but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?
There are several options. Running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also hotswapping of Java class changes). Spring Boot devtools is a cheap way to get quite a big boost (just add it to your classpath). It works by restarting your application in a hot JVM when changes are detected. It also switches off things like thymeleaf caches while it is running, so you don't have to remember to do that yourself. You can use it with an external css/js compiler process if you are writing that code with higher level tools.
Spring Loaded is no longer recommended, but probably still in use. More sophisticated agent-based tools work much better if you need hot swapping with zero delay (e.g. JRebel).
See the docs for some up to date content
but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?
What helped me in IntelliJ 15.0, windows 10, was the following sequence:
STEP 1: Added the following dependency in pom (This is mentioned everywhere but this alone dint solve it), as mentioned by #jonashackt
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
STEP 2: Then from File->Settings-> Build-Execution-Deployment -> Compiler (make sure main compiler option is selected and not any of its sub-options)
enable Make Project Automatically. Click ok and close the dialog
Note : In latest version it will be Build project automatically
STEP 3: Hold Shift+Ctrl+A (on windows) you will see a search dialog with title "Enter Action or option name", type registry. Double click the first option that says "Registry..." it will open another window. Look for the following option:
compiler.automake.allow.when.app.running
and enable it, click close
STEP 4: Restart IDE
Elaborated from this source
You can get hot swapping:
for java code: using spring-loaded
for Thymeleaf templates: disabling the cache
Check this post to see more details: http://blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/
I do not know how far this kind of support goes, but in case you use Eclipse IDE (or anyone reading this): starting up your Spring-Boot application via m2e in debug-mode (press the "Debug"-dropdown button and pick your maven run configuration item).
It works for me like a charm.
My maven run configuration item is configured as follows:
goal is set to "spring-boot:run"
base directory is the project directory
I am not using any further libraries (not even spring-boot-devtools).
That's it.
You can also use JRebel - it will reload all changes (better hotswap) including spring beans. It is easily integratred with both Intellij and Eclipse.
Assuming you are using gradle; use the following config in your build.gradle
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'
applicationDefaultJvmArgs = ["-agentlib:jdwp=transport=dt_socket,address=localhost:7000,server=y,suspend=n"]
mainClassName = "package.ApplicationRunner"
Run the application from the IDE or command line using the command gradle build run
Now the IDE can connect to the remote JVM (on port 7000) where the spring boot application runs. It also supports hot deployment of static files.
or even you can run the main class from intelliJ if the dependencies are properly managed in the IDE. The main class is the class that contains the main method which will call SpringApplication.run("classpath:/applicationContext.xml", args);
In Intellij, I can get this behavior. When the program is running in debug mode, select Run > Reload Changed Classes
Note: After Intellij completes the action, it might say Loaded classes are up to date. Nothing to reload. This is misleading, because it actually DID reload your classpath resources.
My environment/setup includes:
Intellij 13
Embedded Tomcat
Run/Debug configuration of type 'Application' (which just uses a main class)
Serving static html, css and js (no jsp)
try using this spring-boot-devtools tag in pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
http://mytechnologythought.blogspot.com/2017/07/how-to-run-server-of-spring-boot-auto.html
From 1.3.0. (now in Milestone 2) on, you can use the spring-boot-devtools for that as a lightweigt approach - see the docs or this blogpost.
Simply upgrade to >= 1.3.0. and add the following to your pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
Than start your SpringBootApplication with Run As... and you´re fine.
I recommend Thymeleaf (template engine), jRebel for personal developer.
Thymeleaf template files are just HTML resources. So, they`re changed immediately after you edit template files.
If you're using maven, the spring-boot-maven-plugin in your pom.xml needs to be like this to get the hot swap:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
and if you're using thymeleaf, add this to your application properties:
spring.thymeleaf.cache=false
But remember something: Don't use this in your production environment..
How to perform Hot Swap in Springboot Application
When using gradle include following in the dependency:
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.1.RELEASE' & providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
In application.properties add the property spring.devtools.restart.additional-paths=.
Build Gradle and then run application as bootRun
The application is ready to perform hot swap on modification of classes
I'm building a web application with Eclipse using Maven. The server is going to be Apache Tomcat. Eclipse already has tomcat 6 libraries which you can include in your build path and Web Application Module facet to be chosen. That's the way I work without Maven.
However, Maven is able to include the required dependencies to use them in tomcat. My question is, what is the right thing, not to include them via Maven and continue doing that like before, or not to configure eclipse build path and make maven solve it?
The best approach for container specific API like the server API is include it in the maven POM however set the dependency as provided scope so it will be available in your class path for eclipse however maven will not package it in the WAR file when generating it. e.G.
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<scope>provided</scope>
</dependency>
As far as I know Maven do not solve it, if you do not specifically say that your project needs these dependences. The best way is to configure the pom.xml and set the dependencies and build the project using Maven and edit it in eclipse. So once you are done you can easily "package" (build the war) and deploy it in any server location.