how to run a spring application in tomcat without using eclipse etc - java

my application is running in eclipse but i want to run it on tomcat .i want to run my spring application in tomcat server and don't want to use eclipse or any other tool, so how can i do that and where do i put my various files basically the directory of it, and the complete procedure to do that.
main problem is in the directory structure and the path to be put in the tomcat server to run that application. i tried but it gives the 404 error file not found ,as i am new to the spring framework explain in detail

You are asking a very broad question. But, in an attempt to point you in the right direction please see this article. The link given provides insight into the directory structure of your application.
Now as far as running "outside of eclipse" you should be able to export your project from the "File" menu as a "Web Application Archive" or "WAR" file. This file can then be placed under ${CATALINA_BASE}/webapps and be launched when you start your container.
If you are using a stock configuration and you have an archive named "myapp.war", you can access it on
http://localhost:8080/myapp
I hope this information helps you get to where you need to go.

First export the war file using eclipse as you are using eclipse.
Then follow the procedure
How to deploy a war file in Tomcat 7

Related

How to deploy and access a Java WebSocket endpoint on Heroku without any additional frameworks

I have followed this very basic tutorial for setting up a WebSocket endpoint in Java: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html
Heroku, however, expects me to rely on Play Framework: https://devcenter.heroku.com/articles/play-java-websockets
My question is: how could I deploy the same without any additional frameworks and what procedure should I go through in order to make things work?
The problem you had was this:
The tutorial you followed was made for the GlassFish Application Server but Heroku only supports Tomcat 8 and Jetty. See here: https://devcenter.heroku.com/articles/war-deployment
But don't worry, I ported and tested the tutorial to run with Tomcat 8.
I also added the glassfish implementation of the javax.json specification.
(Make sure to download the implementation and not the spec interfaces only)
You can find it here: http://central.maven.org/maven2/org/glassfish/javax.json/1.0.4/
I also noticed why maybe your index.html didn't work locally: I think it was because the WebSocket URL was hardcoded in the websocket.js file.
I have taken the liberty to fix this by making it dynamic.
Here is the complete NetBeans 8.0.2 project:
http://ray.hulha.net/WebsocketHome.zip
Here is the best way to create a war file from inside NetBeans 7 or 8:
There is one catch however, the Tomcat 8 on Heroku is missing the websocket addon.
But no worries, you can added it manually to the war file.
Here are the steps:
Download the Tomcat websocket addon here:
http://central.maven.org/maven2/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.29/tomcat-embed-websocket-8.0.29.jar
The war file is really just a zip file. So I used 7-zip to open the war file. Then navigate to the WEB-INF/lib folder inside the war file.
Copy the jar into the war. Drag and Drop the tomcat-embed-websocket-8.0.29.jar into the lib folder of the war file inside 7-Zip.
Z-Zip will ask if you really want to copy the jar into the war file.
Say Yes.
Here is the compiled war file complete with the tomcat-embed-websocket-8.0.29.jar ready to deploy on Heroku:
http://ray.hulha.net/WebsocketHome.war
I used this command to deploy it:
heroku war:deploy WebsocketHome.war --app websockethome
Make sure to replace the app name in the end with your app name.
And here is the working result:
http://websockethome.herokuapp.com/
Heroku does not require Play framework to use Websockets. That is just an example. As long as your app from the Oracle tutorial binds to $PORT it should work.

What happens behind the scenes of deploying a Spring MVC application to tomcat outside of eclipse?

I guess a drawback of using such an awesome IDE like eclipse is that you miss the point for what happens behind the scenes of an application. I'm a ruby developer so not a java veteran. So I've been coding a project in java and using the spring framework for IOC and MVC. Can someone explain to me what is going on when I select run on server in eclipse? Because eventually I will be deploying this masterpiece of an application to a Linux server. Here is my setup. I am using Spring MVC 3 and the maven plugin in eclipse. In the pom.xml file, I have stuff like latest spring release version, log4j, spring mvc, spring context etc.
I have been testing my application on localhost using the handy option of run on server in the eclipse IDE. The server configuration in eclipse is pointing to the tomcat directory location for where I have installed tomcat 7. Please demystify what happens behind the scenes and what I will need to do if I want to deploy this application on a production server. The more detail the better. Thanks a ton in advance.
Deploying a web application to Tomcat is as simple as this (assuming Tomcat is installed)
Bundle your application in a .war with the correct format.
Move the generated .war file to the /webapps directory of your Tomcat installation folder.
Run the /bin/startup.[sh|bat] script in the Tomcat installation folder.
Note that there are intermediate steps you can do to configure the deployment, like changing your context path. Go through the Tomcat documentation for details.
In step 3, Tomcat will extract the .war contents to a directory in the /webapps folder with the same name as your .war file. It will use this as the context path. The script itself launches a java process by putting the WEB-INF/[class|lib|...] onto the classpath along with some Tomcat libraries.
So Eclipse basically does all the steps above for you.
Ultimately you are deploying an web application that means you are deploying a war file to the server. Regardless of using frameworks like spring, struts anything.
SO a web application request starts from web.xml file. SO for spring mvc application, you are mapping all request coming from browser to DispatcherServlet and then this guy is responsible to manage whole life cycle of your application.
For more details of how MVC works please see
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
So in order to deploy your application (a war) on server first of all you have to create a war from your source code. You can go to traditional approach to use java given utility like using jar from command prompt or you can use ANT, GRADLE, MAVEN and such build tool that creates war for you in automated way.
Spring is not doing anything extra for you. I believe you to research a bit more on how these tools works.
Once a war is ready for you, you can simply go to tomcat UI and there you will find options to deploy your war.
I hope it helps you.
All the majic happens in two places.
The first is your 'Servers' directory in the root of your Eclipse Package Explorer. These are your server configuration files that Eclipse will use (mostly) when it creates a new server instance.
The second is in the ./metadata/.plugins/org.eclipse.wst.server.core/ file system directory in your Eclipse workspace. This is where the tomcat application is actually deployed by eclipse.
The Tomcat Documentation is pretty good actually and helps explain how to do deployments. FYI, I do not know many people that use the Manager, from my experience most people deploy their applications by hand.

importing an eclipse project from file system, then running on server

I am trying to do the spring security tutorial at this link.
In the section entitled "Running the Tutorial application without Spring Security", I took the following steps (adjusted for differences between the current version and the version that was in use when the tutorial was written):
1.) I downloaded the latest release of the Spring Security Distribution,
2.) found and unzipped a war file in the dist directory called spring-security-samples-tutorial-3.1.2.RELEASE.war
3.) Renamed the resulting folder spring-security-tutorial
4.) Created a general project in eclipse called spring-security-tutorial
5.) Imported all of the contents of the unzipped spring-security-samples-tutorial-3.1.2.RELEASE.war
6.) Right clicked on the project in eclipse and selected configure...convert to maven project
7.) Then right clicked on the project and clicked run as...
But there was no "run on server" option. (I had chosen a general project instead of a Dynamic Web Project in hopes of preserving the file structure of the application to be imported)
I then started to repeat the process, but creating a Dynamic Web Project instead of a general project, and eclipse wants me to select the "src folders on build path". The file structure of the web application has 8 .class files in subfolders of WEB-INF, and I am not able to locate any .java files.
What steps can I take in order to download this and run it in eclipse on the server? With the ability to edit the classes?
I have read that I can select a .class file in eclipse and it will open the bytecode in the editor, so I imagine I can convert the class files to java files easily enough. But doing that within a general project triggers the error message that the class is not part of the classpath, so we need to get it in a working web project that can be run on the tomcat server from within eclipse first, I think.
A war file is a Web application ARchive (basically a zip). It is meant to contain a web application's compiled class files and resources (properties files, jsps, css, html, js, etc.). It a package that can be used by an application server (or servlet container) like Tomcat. Except through a Decompiler, you will not have access to the source code from the compiled .class files.
I doubt you can run this on Eclipse's Tomcat instances. Instead go to your Tomcat installation. Mine is at C:\apache-tomcat-7.0.22. Rename the war to something simple like security.war and place it inside the webapps folder, ie. C:\apache-tomcat-7.0.22\webapps\security.war. Go to C:\apache-tomcat-7.0.22\bin and execute the startup.bat Windows batch file. This script sets up the classpath and launches a Java application containing all the applications in webapps. You can see the startup logs in C:\apache-tomcat-7.0.22\logs\catalina.out (as you would normally in Eclipse console).
You can then go to localhost:8080/security to hit the application. Replace 8080 with whatever port you're configured on. security is the same name as the war file. When Tomcat starts (based on a config parameter) it will extract the war into a package directory under webapps with the same name.
You can play around with configuration settings. Some of the important ones are here.
You can shutdown Tomcat by running the C:\apache-tomcat-7.0.22\bin\shutdown.bat Batch file script. You'll have to do this and restart if you change something in the application (ex. the web.xml or a properties file).
I think the tutorial you linked was meant more for trying security settings than actually changing the source code. I'm sure there are other samples online for Spring security, I just don't know them.

How to remove reference of a jar file in a java based web application?

I have a java based web application, which has certain java files and due to some need, i have added some jar files with in the project and made it project specific.
Now, While deploying them in production, I see, that the web file server (tomcat6) already has those jar files in its global library. Now, How do i remove reference of that jar file it, with out disturbing my code(which is working fine).
I saw in other article saying we just have to change the build path to refer the global library instead of local library.
Finally I have 2 questions.
How to do this?
If i'm working on windows and using path while configuring the build path, will it not be a problem if i deploy it in Unix environment.?
Please suggest. Also, its the problem with servlet-api.jar.
I use eclipse IDE. So how to perform these changes in eclipse?
Open your web project in eclipse and right click on the project. click on Properties and then choose build path and remove the jars you want to remove under the tab 'Libraries' and then export the war and deploy it in your tomcat6 server. if you want to run your web application in eclipse, you have to configure the server libraries by clicking 'Add Library' button in build path and then choose server runtime and choose the tomcat6 server configured in eclipse. Hope this helps

Deployment to Tomcat using War file resulting in ClassNotFoundException

I am developing a web application and have hit a wall and could use some advice. So the application was written by a coworker who is no longer at our company. They wrote a web application for Apache Tomcat with Java and Javascript in the back end. The application makes use of the JDBC api to interface with a SQL Server database. This person did all the development in Eclipse and running it this way.
I am trying to take this web application and move it to a server. I attempted this by using Eclipse to export a WAR file and then placing this within the Tomcat webapps folder. Then when I started Tomcat the program was extracted. So far so good. The website comes up and works well. However, when I try to access the pages which rely upon database info everything is coming up NULL. I went through the Tomcat logs and found that in the standard out the following message was given:
ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
I had assumed that the WAR file would include all dependencies but I am guessing that probably this is not the case. If anyone is experienced, is this what has happened? If anyone out there is aware, is there a way to tell Eclipse to do this? Otherwise, what is my option? I am not a Java dev and so I would not know how to install JDBC if needed.
Any help is appreciated.
Mike
You can do the following
Go to Microsoft JDBC Driver download page and download the JDBC driver and install it to a location.
Open the .war file using a zip utility like 7-zip or winzip.
Copy the sqljdbc.jar from the sqljdb_4.0/enu directory where you installed the downloaded JDBC driver and paste it in WEB-INF/lib of the extracted war file.
Zip it back as .war file and deploy it again.
This will get the application running.
If you want to fix this permanently, then you should add the stop to include sqljdbc.jar to your WEB-INF/lib while building war file, in your build system, i.e. in build.xml if you are using ANT or in your Maven's pom.xml under dependencies section for this particular dependency.
You don't need to do the "Export WAR > copy to tomcat > start tomcat" manually, you could configure eclipse to do the deploy directly in your tomcat installation, firts double click tomcat server, and then select "Use tomcat installation" in the "Server Locations" section.
Make sure that your application contains the SQLServer JDBC driver (sqljdbc4.jar) in your project WebContent/WEB-INF/lib directory (assuming your coworker used the Eclipse "Dynamic Web Project" for the project layout), if not, download from here, unzip and copy it to the mentioned folder, the next time you start tomcat, it will automatically add it for you.
If the project uses the maven project layout (there is a file named pom.xml in the project root folder), use the following instructions to install the dependency in your local repository (there are some disagreements between Maven and Microsoft about licensing and redistribution of the driver, so there is no repo)

Categories