Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a Servlet from command prompt? [duplicate]

Possible Duplicate:
Trying to build from the command line, and produce a WAR file

It's a simple HelloWorld Servlet. I am using Windows 7 and have JDK 1.7.0 and Apache Tomcat 7.0.14 installed.

I don't know where to put files.

I don't want to use an IDE, as I want to learn how things actually work.

like image 879
Nauman Avatar asked Dec 27 '22 11:12

Nauman


1 Answers

I think that's a great idea, and I wish more people did it.

As long as your source code file is in the correct package directory structure it doesn't really matter. See this package tutorial if you need more details on that.

You'll need the Servlet API jar on the classpath when you compile, for example, if you're at the top of your source tree:

javac -cp path/to/servlet-api.jar;. path\to\File.java

You'll end up with your .class file in the same directory as your source, but that's fine for now. See this classpath tutorial if you have problems with your path or classpaths.

There are many places to get a Servlet API jar file, for example, in the Tomcat lib directory.

Once you're done you'll either need to create a WAR file, or deploy your app's components separately. WAR files, and web apps, follow a specific directory layout, documented here.

Nutshell:

. (Project root)
├── WEB-INF
│   ├── classes (Your compiled classes, in project directory hierarchy)
│   │   ├── HelloWorldExample.class
│   │   ├── HelloWorldExample.java
│   │   └── LocalStrings.properties
│   ├── jsp (JSPs used as forward targets; not directory accessible to clients)
│   ├── lib (Third-party libraries, including your own)
│   │   ├── jstl.jar
│   │   └── standard.jar
│   ├── tags
│   │   └── helloWorld.tag
│   └── web.xml (Web app configruation file)
└── index.jsp (JSPs directly accessible from clients)
like image 81
Dave Newton Avatar answered Jan 04 '23 21:01

Dave Newton