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

前端 未结 7 1357
遇见更好的自我
遇见更好的自我 2020-12-07 11:39

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 J

相关标签:
7条回答
  • 2020-12-07 12:32

    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:

    1. Get the Eclipse Java EE version.
    2. Create a new Dynamic Web Project e.g. named foobar.
    3. Drag and drop (or copy/paste) the jar files you need in foobar/WebContent/WEB-INF/lib
    4. Create a new file named foobar/WebContent/index.jsp. In the blank file type <h1>Hello World <%= new java.util.Date() %></h1>
    5. 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.

    0 讨论(0)
提交回复
热议问题