I build a small servlet with Idea and tomcat. Initally the project started out with what looks like maven so I removed the iml file and dropped in a build.gradle which looks like:
The problem is that if I try to test this file tomcat seems to report that it starts ok:
24-Feb-2017 11:06:18.498 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 42 ms
24-Feb-2017 11:06:28.506 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager
24-Feb-2017 11:06:28.891 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager has finished in 369 ms
But if I try to browse to localhost:8080 I get the google chrome page not found (not the usual tomcat page not found).
I am pretty new to all this stuff and not sure whether my issue lies with my idea project setup or the build.gradle.
I assume you are trying to setup a Tomcat configuration in IntelliJ to execute your build script? I don’t think IntelliJ actually evaluates your build script logic to run Tomcat. Can you provide the full source code on GitHub so I can reproduce it?
I am also a little bit confused why you’d want to apply the application plugin if you want to build and run a web application. Can you explain?
If I’d approach the problem you are trying to solve then I’d not mess around with IntelliJ at all. I would apply the Gradle Tomcat plugin and just start Tomcat from the command line.
Yes I am trying to setup a tomcat configuration from IntelliJ (and it was working before I converted to to a gradle project - I think it was maven previously as I removed a iml file).
I can send you privately the source code.
I have removed the application plugin and I would be perhaps even more confused if I tried to do this from CLI.
Here is a log trying to start it from IntelliJ:
C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\catalina.bat run
[2017-02-24 11:32:37,550] Artifact poltest:warexploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE: "C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest"
Using CATALINA_HOME: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4"
Using CATALINA_TMPDIR: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_101"
Using CLASSPATH: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\bootstrap.jar;C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\tomcat-juli.jar"
24-Feb-2017 11:32:38.429 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.4
24-Feb-2017 11:32:38.430 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jul 6 2016 08:43:30 UTC
24-Feb-2017 11:32:38.430 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.4.0
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 10
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 10.0
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: C:\Program Files\Java\jdk1.8.0_101\jre
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_101-b13
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: C:\Users\AlGrant\Documents\apache-tomcat-8.5.4
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest\conf\logging.properties
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Users\AlGrant\Documents\apache-tomcat-8.5.4
24-Feb-2017 11:32:38.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\temp
24-Feb-2017 11:32:38.433 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_101\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Java\jre1.8.0_101\bin;C:\Users\AlGrant\gradle-3.1\bin;C:\Program Files\nodejs;C:\Users\AlGrant\AppData\Roaming\npm\node_modules\htmlhint\lib;C:\Program Files\MySQL\MySQL Utilities 1.6;C:\Program Files (x86)\Skype\Phone;C:\Users\AlGrant\AppData\Local\Microsoft\WindowsApps;C:\Users\AlGrant\AppData\Roaming\npm;.
24-Feb-2017 11:32:38.564 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler [“http-nio-8080”]
24-Feb-2017 11:32:38.625 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
24-Feb-2017 11:32:38.628 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler [“ajp-nio-8009”]
24-Feb-2017 11:32:38.630 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
24-Feb-2017 11:32:38.630 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 506 ms
24-Feb-2017 11:32:38.670 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
24-Feb-2017 11:32:38.670 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
24-Feb-2017 11:32:38.682 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
24-Feb-2017 11:32:38.691 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
24-Feb-2017 11:32:38.693 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 61 ms
Connected to server
[2017-02-24 11:32:39,198] Artifact poltest:warexploded: Artifact is being deployed, please wait…
[2017-02-24 11:32:39,259] Artifact poltest:warexploded: Error during artifact deployment. See server log for details.
[2017-02-24 11:32:39,261] Artifact poltest:warexploded: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: C:\Users\AlGrant\IdeaProjects\poltest\out\artifacts\poltest_warexploded\poltest_warexploded.war not found for the web module.
24-Feb-2017 11:32:48.683 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager
24-Feb-2017 11:32:48.967 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager has finished in 284 ms
I think it would be easier for other contributors and users on the forum if you could just push your code to GitHub instead of sharing it privately.
Some observations about your code:
The build fails at the moment. You still define mainClassName even though the application plugin is not applied.
Your Java source code does not reside in the conventional Java source directory and therefore is not compiled. You’ll need to move it under src/main/java.
The class name you use does not follow usual Java conventions. It should start with an upper-case letter.
All the source code you have under web should be moved under src/main/webapp.
Points 2 and 4 are likely the root cause of your problem.
I wouldn’t count on the manager app to be there. I am not even sure if IntelliJ even installs the manager application by default. I’d rather try to get to your application. Do you even see compiled classes under build/classes/main now? If you don’t then you still have an issue with where you put your Java source files. Try to reach the context of your application.
Keep in mind, IntelliJ might handle the execution of your web application completely different from your Gradle setup. How important is it to you to get your web application running from the IDE?
IntelliJ compiles code to the out folder where as Gradle compiles code to src/main/java. I am not sure how good the integration of IntelliJ is in regards to picking up the correct classes and resources when starting a servlet container like Tomcat. It simply might not work at all. That’s why I am suggesting to completely rely on Gradle for that purpose and simply use a Gradle plugin to bring up the container. It would mean starting Tomcat from the command line with a task instead of from the IDE. That would at least unblock you and we could work on the IDE integration as the next thing.
BTW: Your code can’t possibly compile as you do not define a dependency on the Servlet API.
Before we continue with this please upload your code to GitHub so we can properly collaborate. Go ahead and obfuscate your code as needed.
I can not find a war when I run the gradle task to build the war on my disk (I tried to search for it) - there is nothing in the task output that shows the build failed:
I have also noted that if I use the war task no war seems to be created.
If i use the Idea task to build the war i can manually copy it to webapps and start tomcat and I see it deployed, but when I browse to the context I get 404.
I looked in the expanded webapps folder for poltest and I notice there are no files containing the static content that I wanted my servlet to serve up on a Get.
So idea is not including the index.html which is under \WEB\ ?
I touched up your project and got it working in a Tomcat instance declared through IntelliJ. For the purpose I sent you a pull request: https://github.com/bigalnz/ptest/pull/1. Just merge it and you should be fine. A lot of the issue you are facing involve around the basic knowledge about Gradle conventions. Please read the following pages to get up to speed:
Generate the web application archive with ./gradlew build.
Configure Tomcat and point to the generated archive under build/libs.
Start Tomcat. Your index page and your /form page should work.
As you can see we are using a lot of the files that have been generated by the Gradle build and not IntelliJ. That’s what I meant with interoperability issues earlier. I am sure you can also configure the web artifact in IntelliJ and use that. However, that’s more a question for JetBrains. I am not sure where they stand with the functionality.
Thank you. I have created a project from scratch using the IDE and, then the gradle plugin in the IDE to create most of the project structure and then added the html files and java classes. It compiled and worked as expected.
I copied the war to my tomcat installation, but this will quickly become tedious so I will configure tomcat to look in build/libs.
In steps 1 and 2 these commands are issued through the CLI (I am on windows) but did you create the project first in idea and cd to that before issuing these commands? I got:
FAILURE: Build failed with an excpetion
*What went wrong:
Task ‘idea’ not found in root project “myname”
I created the project files with the Idea plugin from the command line. I added that in my pull request or it was already there. Either way both ways should work.
You will likely need to add the remote debugging options to the Tomcat process. Everything else should work as usual. The configuration is not really relevant to Gradle as you not running the Gradle process in this case. I’ll let the community take it from here. Maybe you can also ask at JetBrain’s support site.