Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to find class

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me what is wrong in the following 2 lines of code?
This is in a JSP.
<%
A obj = new A();
obj.greeting();
%>

I have a class A which has been stored under the classes subdirectory in Tomcat.
Getting an exception - Unable to find class A.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you import the class?
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did.
In the line on top:
import="java.util.*, A.java"
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't specify the "java" portion when you import. Simply importing "A" will suffice. In addition, the JSP container might not be able to compile the JSP document when you try and import classes that are part of the default package.
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import= "A" errorPage="" %>
Still getting 3 errors-
C:\Tomcat\work\Standalone\localhost\app\mypage_jsp.java:8: '.' expected
import A;
^
C:\Tomcat\work\Standalone\localhost\app\mypage_jsp.java:116: cannot resolve symbol
symbol : class A
location: class org.apache.jsp.mypage_jsp
A obj = new A();
^
An error occurred at line: 57 in the jsp file: /mypage.jsp
Generated servlet error:
C:\Tomcat\work\Standalone\localhost\app\mypage_jsp.java:116: cannot resolve symbol
symbol : class A
location: class org.apache.jsp.mypage_jsp
A obj = new A();
Can you please help me?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucky,
As I said, Tomcat can't compile a JSP document that tries to import classes that are defined in the default package. (Like your "A" class!) You need to put "A" in an actual package (e.g. com.yourdomain.A). Then it will work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic