Classes not seeing each other in JSP

本秂侑毒 提交于 2019-12-02 07:36:37

I don't understand why some people are so against using Eclipse or Netbeans (or any IDE). I realise some people prefer manual drive to automatic because you tend to feel the raw power of the engine. I learnt to drive using automatic and then got used to driving manual. I am proficient manual driver now thro learning driving with automatic. I can even handle excavators now, rather proficiently (I dug the hole for the house I am building).

Same with Java. You let Netbeans or Eclipse construct the whole web-app structure for you and then you inspect the structure. Play around with the ant build. Learn from the IDE by mucking around with it.

I know that some people believe that without an IDE, you would expose yourself to the raw compilation process and hence would have a "deeper" understanding of the build process. REALLY? Don't waste your time. Let the IDE do it for you. You will learn the build process faster if you are willing to inspect the files and structure produced by the IDE.

Run this in your classes folder:

javac -cp /usr/local/tomcat/webapps/myProjectName/WEB-INF/classes *.java

You need to include the classpath during compile time.

You should not have .java source files anywhere under WEB-INF. Keep compilation and packaging separate.

There are some other errors apart from not recognizing classes, but when it comes to your question:

  • do not add anything to classpath, it's irrelevant,
  • do not change any system settings, they are irrelevant,
  • do not worry about packaging just yet - you are still learning the basics,
  • do not use the javac line by Zeki, it won't work (*.java would only compile files in the classes/ directory, and you want to compile classes under mypackage).

And most important of all: do not think it's hard or that it requires a complicated setup. It's really easy, and it's very important to get right once you start studying Java.

When javac compiles a class and it finds that it uses some other class, it looks for it in two places, called classpath (for a compiled version) and sourcepath (for sources). Both of them, by default, include the current working directory. Note that it is not the same as the directory that contains the .java file.

The exact location of .java or .class file inside the classpath always depends on the package (but you knew that already). In your case, if you do:

cd WEB-INF/classes/mypackage
javac class1.java

your sourcepath is WEB-INF/classes/mypackage, the needed class is mypackage.class2, so, against intuition, javac will look for file WEB-INF/classes/mypackage/mypackage/class2.java. Such file does not exist, and you get an error.

On the other hand, imagine:

cd WEB-INF/classes
javac mypackage/class1.java

now your sourcepath is WEB-INF/classes, the needed class is mypackage.class2, javac will look for file WEB-INF/classes/mypackage/class2.java - and it will find it without problems.

The moral: when you compile, always write javac in the top directory of your sources, above all the packages. This way your packages align with directories, as seen by javac, and the compilation is flawless.

After you manage to exactly understand everything happening, by all means - switch to Netbeans (and, in a parallel thread, learn ant, then maven). It installs with it's own copy of tomcat 6, which you can use to develop your app; tomcat 7 is downwards compatible, so installing your app to it will be just the matter of copying war file (built by netbeans) to your tomcat 7 webapps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!