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

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.

Related

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

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

What happens when you run a project on a local Tomcat server in Eclipse? [duplicate]

I am making a java webapplication using eclipse and tomcat server. I want to know what happens when I run my website on Tomcat ? What are the steps Eclipse does in the background to run the application on tomcat.
This will help me understand when to switch off the server (while debugging) / when to clean the server etc.
I need to know what goes into the server so that I can get better at debugging.
Right now all I do is restart the server everytime something goes wrong. I have wasted enough time doing that. I guess I need to invest a little bit more time in understanding what happens behind the scenes.
Environment assumptions
I will assume:
target/classes is the target folder for compiled classes
src/main/webapp is the web application content folder
Project > Build Automatically option is checked
Deployment directory
Eclipse is using exploded WAR deployment - i.e. the deployed application is deployed as a folder, not a single file archive. Application files are placed and loaded from ${workspace}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/.
Publishing
Publishing is a central process which is responsible for assembling and deploying the web application. When talking about local Tomcat, this means copying "web content, compiled classes, libraries, ..." into deployment directory (the one in .metadata).
Eclipse is able to do partial publishing - i.e. when a single resource changes (e.g. some JSP), Eclipse will publish only that single file.
By default publish process is performed automatically when some resource changes. This can be modified in server settings (double click on the server name in Servers view).
Changing static resource
If you change lets say src/main/webapp/resources/myApp/css/main.css:
upon publish the file gets copied to the deployment folder
resource is instantly available to server clients
Changing JSP file
If you change JSP file:
upon publish the file gets copied to the deployment folder
Tomcat notices that the JSP file has changed and recompiles it
changed JSP is ready to render content
Changing Java file
If you change a java source file:
the file gets compiled into target/classes
upon publish the file gets copied to the deployment folder
Tomcat notices that a class file was changed and reloads the context (i.e. web application is restarted)
You can turn of the auto-reloading feature in server settings on the Modules tab. Without auto-reloading you can still use hot swap feature, which is able to replace code in running JVM. This is possible only when method signatures are not changed.
If you want more advanced solution (i.e. not limited to changing just a method body) when it comes to reloading java changes, you should check projects like JRebel (not free).
Cleaning
Deployed application can get corrupted. It is worth noting, that when you want to clean completely compiled and published resources, you should:
Clean the compiled classes (Project > Clean... - deletes target/classes)
Clean the deployed files (Server > Clean... - deletes deployment folder)
Clean Tomcat working directory (Server > Clean Tomcat Work Directory... - deletes compiled JSPs)

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

How do I properly deploy and structure projects in Eclipse against Tomcat?

I am relatively new Java developer that's been thrown in the deep end, my usual skillset lies in Microsoft products and C#.
However I have managed to write a nice web service using Restlet and incorporating Toplink and doing some database CRUD stuff, etc etc.
I developed this in Eclipse against Tomcat 5.5. I followed tutorials and examples and managed to throw something together that works.
I had all my jar files in WebContent/WEB-INF/lib directory of my project - I soon realised that when I export this as a WAR file and deploy in Tomcat, it takes these jar files with it and stores them local to the application.
Fine, but then when you try to undeploy the app, it only does it partially as Tomcat "holds on" to some of the jars it was using i.e. the Oracle JDBC, and Toplink ones. This makes sense as the web service was using these jars as there was a live Oracle connnection going on.
So then I thought I should have all these jars in a common place where all deployed apps can access them, that folder I believe is:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib
(in my case). This made total sense, it's logical as you don't want loads of apps on Tomcat all referencing the same jars. So I removed all the jars out of my projects lib folder and put them in the common\lib directory.
Now my app will not work properly - it can't find my source code in src, the custom bespoke code that I have programmed against these jars. It will only work if I jar up my source code and put in the directory stated above. Odd.
I am obviously very confused with class paths and build paths and all of that stuff, and must have got this sort of stuff all wrong as I am no Java expert, as I readily admit I have pretty much hacked this together - so can anyone explain to me in laymans terms how I should structure my project to get it working with jars held in a common folder in Tomcat. Or are there any good resoureces on the web to help explain to me what I should do.
Hope this all makes sense...
Here is a pic of my current project:
Sounds like you are not using WTP web project.
If you've installed 'Eclipse IDE for Java EE Developers' flavor of Eclipse, the WTP is already bundled in there. Otherwise here its update site -> http://download.eclipse.org/webtools/updates
Once you've installed WTP you should create 'Dynamic Web Project'.
In general I would recomment to put only your presentation layer here ( JSPs, CSS, HTML ) and put all pure java projects into standard 'Java' project that you later add as a dependency to 'Dynamic Web Project'.
WTP can run your web application under a number of containers, fortunately Tomcat is supported.
You will need to configure it through Window->Preferences->Server->Runtime Environments.
Once runtime envirnonment is configured, you can create your server runtime:
Right click in 'Servers' view.
Choose New->Server
Select server type: 'Tomcat v.5.5 Server'
(Optional) Change server name to whatever makes sense for you
Select server runtime environment: This is the Runtime Environemtn that you have configured previously
Hit 'Next' button
Add your 'Dynamic Web Project' project to 'Configured projects:' panel
Hit 'Finish' button
After the server is configured you can just run it, or you can put it in Debug mode. All source code referenced by dependent projects will be available for debugging.
NOTE:
From personal experience, I would not recommend using common/lib. Put all the jars that your web application relies upon into its WAR file. If you are worried about dependency tracking then start looking into Maven and m2eclipse.
Usually I put my jars in $CATALINA_BASE/shared/lib/
Please read this documentation for more clarification and specially Class Loader Definitions section.
Also restart the server after you copied the jars.
Go to Window > Show View > Other > Server > Servers, a servers tab will appear below with console tab. Click on your server and then F3, this will open server configuration.
Check Server Locations, and make sure you checked Use tomcat installation(takes control of tomcat installation) then click Modules tab below and it will show your installed modules, make sure the module is present or add it.

CLASSPATH, Java Buld Path (eclipse), and WEB-INF\LIB : what to use, when, and why?

I recently switched to J2EE from .NET, and am confused about where to put JAR files. I know that the CLASSPATH, WEB-INF, and Eclipse's Java Web Path are all places where JARs can be stored, but I'm confused about which folder to use, when, and why.
First off, we have the CLASSPATH. I usually set this by going into "Environment Variables" inside "My Computer." I know that this is the default place where the Java compiler looks for JAR files. When I add a folder or a JAR to my CLASSPATH environment variable, why is it ignored by Eclipse, the Java compiler, and the web server?
Also, I know that WEB-INF\LIB is a place where you can put JAR files that your web app is going to use. However, I've put JARs in WEB-INF\LIB only to have them be ignored. In what situations should I put JARs into WEB-INF\LIB folder? How do I get Eclipse or the web server to notice them?
So far, the only thing that works for me is to actually change the Java Build Path for an Eclipse project. I'll select the JARs I need and hit "Add External JARs." Most of the time when I do this, Eclipse will recognize my JARs and read the classes therein. However, I've run into a bunch of weird random errors while doing this (mostly having to do with dependencies, I think). For some reason, I just get the feeling that this isn't the right way to do things, and that I'm missing some vital piece of information. When should I be manually Adding External JARs inside Eclipse, and when should I be doing things differently? How come Eclipse's Java Build Path doesn't seem to know about the folders in my CLASSPATH environment variable?
Really, I would just like to gain a better understanding of the CLASSPATH, Eclipse's Java Build Path, and the WEB-INF/LIB folder -- the purposes they serve, the relationships between them, and where I should be putting my JARs in various situations. I would appreciate any advice you could give me, or any articles that you could recommend.
Thank you.
The CLASSPATH you set in your environment affects only standalone Java applications, i.e. ones you run from a command prompt or an icon. As you've noticed, Eclipse ignores this. It sets up its own per-project classpaths.
javac and java, if called from the command prompt, should/may honor this path, but it's no longer considered great practice to do this. It's turned out that every app needs its own set of stuff, so a global CLASSPATH isn't really doing any of them any good. Modern practice is to simply specify the classpath with the -cp option on the command line for javac or java.
A standalone Web Application server will also set up its own classpath. From the command line or GUI, WebAppServers are usually started by a script (.BAT or .sh) that sets up a classpath using -cp. Tomcat has a directory called common or common/lib where it expects to see libraries that should be available the the server and all programs running under it. But you will generally not need/want to mess with this, as it's customaries for applications to provide their own library collectons in WEB-INF/lib.
So for a Web app, you'd put your varous jars into the lib directory, under WEB-INF, assuming Eclipse pre-builds such a directory structure for you.
All the libs you need also need to be made known to Eclipse. In the Project Explorer, I select the whole slew of them at once, right-click and select Build Path | add to build path. That's easier than messing with Eclipse's project build path manually.
Java has a long history and experience has shown that some ideas were good and some were bad.
The CLASSPATH environment variable was the initial way to tell the Java machine where to locate classes from your program, and works reasonably well for command line programs. It was rapidly found that this should not be a global thing (as that tend to mess things up in the long run) but a per-program thing. This could be done by creating a wrapper script/BAT-file which sets the variable and runs the Java machine.
All was well, then people wanted to write web server stuff in Java. The Servlet API was created where a web application is a stand-alone unit - this resulted in that the CLASSPATH for each web application is the unpacked files under WEB-INF/classes plus the jar-files under WEB-INF/lib. And only that. This means the global CLASSPATH variable is ignored. This has been found to be a VERY good thing, so the concept has migrated elsewhere.
For instance a "executable jar" (which Eclipse calls a "runnable jar") which is invoked with "java -jar foobar.jar" contains the complete classpath INSIDE the Jar in a special manifest file. Java Web Start which is used to start java programs from a web server explicily lists the full classpath in the configuration file on the server.
But, to get you started. If you want to write a Java web application:
Get the Eclipse Java EE version.
Create a new Dynamic Web Project e.g. named foobar.
Drag and drop (or copy/paste) the jar files you need in foobar/WebContent/WEB-INF/lib
Create a new file named foobar/WebContent/index.jsp. In the blank file type <h1>Hello World <%= new java.util.Date() %></h1>
Right click in editor for index.jsp, choose Run -> Run on Server, and choose the Basic -> J2EE preview at localhost server, and Finish.
A browser window will now open, either in a browser or inside Eclipse which will render your JSP-page. You can change the JSP-page, save it with Ctrl-S and reload the browser window to see the changes.
Also, I know that WEB-INF\LIB is a place where you can put JAR files that your web app is going to use. However, I've put JARs in WEB-INF\LIB only to have them be ignored. In what situations should I put JARs into WEB-INF\LIB folder? How do I get Eclipse or the web server to notice them?
The real problem you have here is likely that you didn't got Eclipse for Java EE developers and/or that you just created a generic Java Project instead of a Dynamic Web Project and built up the necessary folder structure yourself.
If you create a Dynamic Web Project in Eclipse for Java EE developers, then Eclipse will automagically add any libraries in WEB-INF/lib to the build path. The build path is roughly said just the classpath which is been used in both compiletime and runtime. With other words: just drop the 3rd party JAR's in there, really nothing more needs to be done.
Note that Java is case sensitive, thus it should be really called WEB-INF/lib, not WEB-INF/LIB. But anyway, if you create a Dynamic Web Project, then Eclipse will just automagically generate the correct folder/file structure for you.
As said by others, ignore the %CLASSPATH% environment variable. It is only used by javac.exe/java.exe and even then only when you do not specify any of the -cp, -classpath or -jar arguments. In real world this environment variable is seldom used, it is just some convenience for starters (and unfortunately also the most confusing one, they should never have invented it).
If you're dealing with web applications, /WEB-INF/lib is the portable place to put JARs. This is where web servers servlet containers expect to find an application's jar files.
Eclipse requires you to specify the path to your libraries, jar files (on Properties -> Java Build Path -> Libraries tab). This can be found on the .classpath project file.
Usually you have the JRE libs on its path (which would be on your classpath too), so adding the libs to the classpath and updating eclipse build path would work.
The WEB-INF directory should be the place that contains necessary information for your web application.
I'm not an Eclipse expert, but I think that your problem can be answered like this:
1) CLASSPATH is an environment variable that is read in when you launch java programs and is used by classloader to figure out where the classes are placed.
I would modify the CLASSPATH variable only in the case when you are launching an java program from a script, as this allows you to conveniently launch a program and make sure that the classes are found. This would not be the case for you, as you are developing the web application.
2) WEB-INF/lib is the directory under which the web application container's classloader (like tomcat or glassfish) looks into if your web application needs to resolve a class. So you put there the classes that are used in your web application.
Some IDE's do include the libraries/.jar files that you are using in a project automatically to the package.
3) Eclipse library/classpath resolving during the development time. I would assume, but apologies for assuming, as one really shouldn't do this ;), that you can define a library (add external .jar files to projects) and autocomplete/all the other interesting features should start working with this, as you basically make those classes visible for the IDE with that activity. I also would assume that you can then mark those libraries to be automatically added to the web projects etc., by the IDE.
In general a good reading about how classes are found during execution is here (it's a sun's official documentation). Also a good place to read about this is the ClassLoader class documentation.
Taken together the comments helped me as well. I had added all the jena .jars to the build path from eclipse but that wasn't sufficient. Following suggestion to "add to WEB-INF/lib" it seemed intuitive to drag from libraries folder to WEB-INF (from within eclipse), but that didn't work. Nor did copying the .jars to WEB-INF. I eventually drag-and-dropped from the windows desktop to the WEB-INF lib folder in Eclipse, and that fixed the problem. It would be nice if any .jars added to the build path were automatically copied to WEB-INF lib by Eclipse. In case it matters, this was eclipse EE IDE, Indigo release, on windows 7.

Categories