Classes not seeing each other in JSP
In a tomcat JSP application I have this directory layout:
webapps/
myProjectName/
index.jsp
WEB-INF/
classes/
mypackage/
class1.java
class2.java
I'm trying to compile class1.java which references class2.java. It's coded in a form sort of like this:
package mypackage;
public class class1 extends class2 {}
and class2 looks like this:
package mypackage;
public class class2 {}
however, I get an error on class1 saying that class2 cannot be found. First I compiled class2, which compiled just fine, but when I tried to compile class1 it failed, saying that class2 couldn't be found. I tried adding the directory to my classpath (ubuntu) by adding this to /etc/enviornment:
/usr/local/tomcat/webapps/myProjectName/WEB-INF/classes
but it still doesn't compile.
Any idea what's wrong?
The exact error output is this:
javac "Page.java" (in directory: /usr/local/tomcat/webapps/developers/WEB-INF/classes/library)
Page.java:20: cannot find symbol
symbol : class DynamicPage
location: class library.Page
DynamicPage dynamicClasses = {new registerPage()};
^
Page.java:20: illegal initializer for <none>
DynamicPage dynamicClasses = {new registerPage()};
^
Page.java:20: cannot find symbol
symbol : class registerPage
location: class library.Page
DynamicPage dynamicClasses = {new re开发者_如何学PythongisterPage()};
^
Page.java:34: cannot find symbol
symbol : class DynamicPage
location: class library.Page
DynamicPage selected = null;
^
Page.java:35: cannot find symbol
symbol : class DynamicPage
location: class library.Page
for (DynamicPage dp: dynamicClasses) {
^
5 errors
Compilation failed.
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.
精彩评论