ClassNotFoundException on Tomcat Server - tomcat7

I am using this tutorial to set up Tomcat Server. After I have put the HelloServlet.java in classes and Web.xml in the WEB-INF folder and I'm giving the command
localhost:9999/hello/sayhello
On the browser. I'm always getting ClassNotFoundException. If anyone can tell me where am I going wrong.
I'm using JDK1.6.0_30, and Tomcat7 for my sample application.

You need to put the compiled HelloServlet.class file (not the .java file) in the WEB-INF/classes folder.

Compile HelloServlet.java and place the output class file HelloServlet.class into
<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.class

One thing you should check is that, the HelloServlet.class should be in a package. Looks like you have a long way to go with Servlet and JSP... I recommend Head First Servlet and JSP for your reference.

In your 'WEB-INF' of 'classes' folder place the .class files and if your using JDBC, jsp's just Copy the .jar executable files into 'lib' folder. and make sure that xml file should contain the proper information.
you should follow below Web Application Directory Structure
WEB-INF/ --
web.xml --xml file
classes/ ---classes folder here we keep .class files Myservlet.class
lib/ ---lib folder here we keep all .jar files. Myapp.jar
Welcome.html
Welcome.jsp

Related

where does jetty serve files from

I just installed jetty 9.4.6. I dropped one of the war files from the demo area into $JETTY_BASE/webapps. The war file got expanded into /tmp/jetty-10.1.100.103-8080-test.war-_test-any-4774924669679182185.dir
I then tried a curl on it like this: :8080/test/index.html. I get a 503 error. I verified that index.html exists.
So does jetty serve the compressed war file or does it serve from the tmp folder? Any doc links would be greatly appreciated.
Yes, it creates a temp directory, though you can also define and persist your own if you'd like.
By default, Jetty will create this directory inside the directory
named by the java.io.tmpdir System property. You can instruct Jetty to
use a different parent directory by setting the context attribute
org.eclipse.jetty.webapp.basetempdir to the name of the desired parent
directory. The directory named by this attribute must exist and be
writeable.
You can find more in the official documentation here.

Deploy a embedded Jetty&Jersey war to jetty9,only can see static file?

I cteate a jetty&jersey embedded project with IDEA and Maven,I put it in Github https://github.com/Mengqi777/JettyProject. Run JettyServerStart.java start the server, in the browser address bar enter localhost:8080/dynamic, show OK, enter localhost:8080/static, show static resource file
Now I package it to a war file,and put it in jetty webapps directory. But only can see static file in brower with enter localhost:8080/static.404 Not Found Error when in brower with enter localhost:8080/dynamic
What happend?
What should I do to package this project into a war file or jar file and run it in jetty successfully?
You are doing things in your embedded-jetty usage in JettyServerStart.java that you do not declare/configure in your webapp or war file.
It's a maven project, but not declared as a webapp or war project (in the pom.xml)
Its doesn't have its WEB-INF in the right place (maven directory structure wise), which means your built war file is invalid.
The dependencies are not declared correctly for a webapp or war project (you cannot include jetty-server in a war file)
Your badly located WEB-INF/web.xml does not perform the same configuration steps as your JettyServerStart.java
You don't specify how you created your ${jetty.base} directory to support this war file?
You didn't specify what version of jetty-distribution or jetty-home you downloaded, or are attempting to work with.
The statement "and put it in jetty webapps directory" is unclear, which one? (using jetty-distribution/webapps is invalid and will cause many errors and warnings if you attempt to use it for your own webapps, there's no jetty-home/webapps, and you didn't identify your jetty-base configuration)
The way your project is declared right now, even if manually assembled, skipping maven entirely, you have no servlets, no filters, no listeners, no intializers, only a servlet spec mandated DefaultServlet on url-pattern / giving you static content. That's why accessing http://localhost:8080/static/ works, but nothing else.

Maven. How to create .jar with local .txt files

I use Intellij Idea.
Java-classes use .txt files which are placed in folder where .java files are situated.
I created .jar file with maven and pom.xml. But there are no .txt in .jar file.
How to add them there?
UPDATED:
I have such structure src/main/java/Project-name/ for .java files. And src/main/resources/for .txt files. Within .java files I access files as ../../resources/filename.xml and all works fine. But .jar-files created with maven says:
Unable to open "../../resources/filename.xml"
How correctly should I declare filenames?
You are going against the convention. Java source goes to src/main/java but your resources must go to src/main/resources. The rest ist handled by Maven.

How do I add a directory in /target to the resulting JAR with Spring Boot?

I'm using Enunciate to generate REST documentation upon building a REST application. Enunicate outputs documentation to /target/docs. I would like to add the /docs directory to the resulting JAR file (and rename it) to be able to serve docs as static content.
How do I do this? I can't figure out how to get these static files (which are generated upon build) into the JAR.
I guess you can solve this by configuring the Maven plugin for enunciate and wiring it up to be run in the 'generate-resources' lifecycle phase.
Also, make sure you set the output-dir to a subdirectory of src/main/resources/static, as commented by Rob above.
I added this to my enunciate.xml to force the docs directory to be generated in a custom location which will be packaged with the .war file
<docs docsDir="target/<app_name>/docs"/>
and then maven will put the entire contents of target/ into the resulting war file package

serving files from a spring boot war file

I've followed the guide here for turning a "hello, world" level Spring Boot app to a war file. I can run this war like a jar and it will return the simple template.
What I don't understand is why I can't access a main.css file I've created. I've placed it in the resources directory under "static/css/main.css" and according to the docs here Spring Boot will automatically server files under "resources", "static", "public", and "META-INF/resources". However, when I build my war file and run it I can't query those files in the browser (like http://localhost:8080/static/css/main.css). Have a missed a step? If I peek into the created war file I see the "static" directory in "WEBINF/classes" right beside the "templates" directory and the directory holding my application.
Files in src/main/resources/static are served from / so you don't need static in the path. You CSS file should be available from http://localhost:8080/css/main.css

Resources