0

guys,I am facing a basic but tricky problem.

Recently I am learning JSP.

As we know the "import" sentence help the java program find the class in system java library or your own file.

Here is an example to illustrate my problem: The imagine have some problem,I will upload it later the day .

Assuming I have App.java in path

$main/com/sub

,in the IDE it will belong to package com.sub; and I have another useApp.java in path

$main/com

,it will be allocated with the package com. Now if useApp.java need to import App.class.As my textbook say,the manually way of import the java class is to put the .class file in the subdirectory of the src directory .Then in this way the App.class should be located at

$main/com/com/sub/App.class

It is very werid,isn't it.This is the structure I use first.Now I know organize the different class in this way is terrible.So is there any convention way to organize your own file's dependencay .Should I just configure the .classpath file of the project in general?

I have trying to use .classpath file.Got the useApp.class file set.and try to invoke it in jsp file use <%@ page import="fullclassname"%> And keep getting the error"can not resolve to a type".

1 Answers1

0

Classpath is not as straight simple as it seems like on the first glance. Especially Web applications and apps using plugin mechanisms use different classloaders, which can be confusing.

Classloaders

A good starting point is some reads about classloaders: https://www.baeldung.com/java-classloaders This will answer the question how "import will find the file" in an overall Java way.

Debugging Classloading

It you need to "find the file" while debuggin your application you can start your java application with the -verbose option (also in an IDE, normally called launch configuration or similar).

java -verbose HelloWorld

https://www.ibm.com/support/pages/using-java-verbose-option-determine-where-jdbc-driver-classes-are-loading https://dzone.com/articles/how-use-verbose-options-java

Standardized Project Structure

In addition there's some standards hot to build Java project structures. Maven was one of the early dominators here. So adopting a standard MAVEN project structure might not be a bad idea for a beginner. https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java



supernova
  • 1,762
  • 1
  • 14
  • 31
  • Although haven't read you answer entirely,thanks for your help.Maybe rely on IDE too much in this situation isn't a good way. – Gearhead Stack Jun 12 '20 at 23:36
  • It turned out that eclipse already treat the reference problem very well.As long as you follow the project's structure.Finally I found when I facing this problem,the collaborate between tomcat and eclipse have problem.So I actually run the Tomcat server stand alone,that might cause the compile issue. – Gearhead Stack Jun 14 '20 at 10:43
  • Running stand alone you can still apply the JVM --verbose option that should do the trick also in Tomcat or whatever Java servlet container. Like described in my answer. It's always good to understand what happens behind the scenes and not rely on an IDE as a prod environment will not take care about you the same way. – supernova Jul 28 '20 at 14:54