class cannot be resolved to a type 或者JSP import class cannot be resolved to

错误调试解析:class cannot be resolved to a type 或者JSP import class cannot be resolved to a type 

import XXX.XXX cannot be resolved to a type , import XXX.XXX cannot be resolved错误处理解析 

类的引用不可定义为一种。 

出现这种问题的情况一般是你的引用出现了二义性。 

比如你引用了classes.pack.num1,但是你同时在根文件夹下有一个classes的文件夹,和把此classes文件打包成的jar包,又或者你的classpath环境变量配置的过程中同时出现了路径"."和“XXX.jar”而有两个同名同路径的.class文件可以被找到,就会引发此问题。 

很多时候用vi编辑.java文件的时候,默认使用的编译存放路径是".",就是说同目录下。 

eclipse工程中默认.java文件的位置是src,编译文件.class的位置是classes,这种情况下, 使用手动测试的时候会出现在src的文件夹下也会生成出.class文件,如果系统环境变量中有classpath="."的定义时,再次使用eclipse编译时,就会出现class cannot be resolved to a type,因为同时可以找到两个同名同路径的.class文件。 

JSP impot class cannot be resolved to a type我的问题出在eclipse编译时把.class文件放在了classes文件夹下,但是tomcat去找的时候,只找了"."文件夹,结果就找不到。而在编写的时候,eclipse是能找到的,所以编写时不报错,运行时报错。如果你在.文件夹下也生成.class文件,tomcat能找到了,eclipse又找重了,还是不行。 

我的解决办法是,在tomcat启动时,为tomcat添加引用路径,先把classes文件夹导入到JVM中。tomcat在调用类的时候,就不会找不到了。 

具体操作方法是,使用eclipse的tomcat插件,在eclipse的主界面菜单window下的preferences对话框中找到tomcat插件的那一项,选择JVMsetting,有一个classpath(before generated classpath)中,导入你的classes文件夹。就可解决此问题。 

相同的,如果tomcat运行时出现了找不到引用的jar包,也可以在这里设置导入。 




结合我自己的jsp出现的问题,因为包结构里的一个包名和同目录里的一个类名名称相同,出现这个异常


Foo cannot be resolved to a type

When you got this error there are probably missing import in your .java file.

For example, code:

1 public class CannotBeResolved { 2 public static void main(String[] args) { 3 List list = 4 new ArrayList(); 5 } 6 }

Produces 2 error - line 3 and line 4:

List cannot be resolved to a type
ArrayList cannot be resolved to a type

When you add imports (lines 1 and 2):

1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class ClassInstanceCreationExpressionTest { 5 public static void main(String[] args) { 6 List list = new ArrayList(); 7 } 8 }

error goes away :-) (You can replace both imports with only one java.util.*, but I'm using Eclipse and CTRL + SHIFT + o shortcut resolves classes = adds import, so I'm not using .* imports)

There is solution without imports, but I preffer the one with imports, code is imho more intelligible.

Without imports:

1 public class CannotBeResolved { 2 public static void main(String[] args) { 3 java.util.List list = new java.util.ArrayList(); 4 } 5 }

Maybe you are asking why there is not import for String class, answer is that java.lang.* classes do not need imports.

原文地址:https://www.cnblogs.com/youxin/p/2212841.html