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

前端 未结 7 1355
遇见更好的自我
遇见更好的自我 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:12

    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.

提交回复
热议问题