LarryAndro
asked on
Declared Package Mismatch
I have a jsp calling some java classes, and am having problems with the package import and declaration statements. I'm using Tomcat and developing with Eclipse. Here's my directory structure...
SubscriptionControlService <-- project directory
---src/classes/java <--- Source java files here
---SCS <-- html,jsp files at this level (This is my webroot)
---SCS/WEB-INF/classes/jav a <-- compiled class files automatically placed here
I am having two reported problems - one in my .java file and one in my .jsp file, but suspect they are reflections of the same problem.
JAVA PROBLEM
-----------------
Here's the package statement at the top of the java file...
package classes.java;
The error reported is... "the declared package does not match the expected package."
JSP PROBLEM
---------------
The imports at top of INDEX.jsp are...
<%@ page import="classes.java.*" %>
<%@ page import="java.util.ArrayLis t,java.uti l.Iterator ,java.util .List,java .lang.*" %>
The reported error for the first import is... "The import classes cannot be resolved"
Can someone explain why I'm getting these errors?
SubscriptionControlService
---src/classes/java <--- Source java files here
---SCS <-- html,jsp files at this level (This is my webroot)
---SCS/WEB-INF/classes/jav
I am having two reported problems - one in my .java file and one in my .jsp file, but suspect they are reflections of the same problem.
JAVA PROBLEM
-----------------
Here's the package statement at the top of the java file...
package classes.java;
The error reported is... "the declared package does not match the expected package."
JSP PROBLEM
---------------
The imports at top of INDEX.jsp are...
<%@ page import="classes.java.*" %>
<%@ page import="java.util.ArrayLis
The reported error for the first import is... "The import classes cannot be resolved"
Can someone explain why I'm getting these errors?
> Here's the package statement at the top of the java file...
>
> package classes.java;
Ahhhh...
Hmmmm... That should be
package java ;
and as I said, just put the classes in WEB-INF/classes/java (as you are doing), and then do the page import as I said in the last comment :-)
>
> package classes.java;
Ahhhh...
Hmmmm... That should be
package java ;
and as I said, just put the classes in WEB-INF/classes/java (as you are doing), and then do the page import as I said in the last comment :-)
ASKER
In my jsp, I changed...
<%@ page import="classes.java.*" %>
<%@ page import="java.util.ArrayLis t,java.uti l.Iterator ,java.util .List,java .lang.*" %>
to...
<%@ page import="java.*" %>
<%@ page import="java.util.ArrayLis t,java.uti l.Iterator ,java.util .List,java .lang.*" %>
and the reported error disappeared. But, now aren't I importing everything in Java's java subdirectory instead of my own java folder?
No matter whether the above is OK or not, I really don't like my choice of 'java' as a folder name. So, I renamed it 'scsclass.' The be consistent, I not have...
<%@ page import="scsclass.*" %>
<%@ page import="java.util.ArrayLis t,java.uti l.Iterator ,java.util .List,java .lang.*" %>
in my jsp. And, in my java source I have...
package scsclass;
No errors occur in my java source file now. But, I do get the following jsp error...
The import scsclass cannot be resolved.
What do you suggest now? Was my changing 'java' to 'scsclass' a good idea, or should I change back?
<%@ page import="classes.java.*" %>
<%@ page import="java.util.ArrayLis
to...
<%@ page import="java.*" %>
<%@ page import="java.util.ArrayLis
and the reported error disappeared. But, now aren't I importing everything in Java's java subdirectory instead of my own java folder?
No matter whether the above is OK or not, I really don't like my choice of 'java' as a folder name. So, I renamed it 'scsclass.' The be consistent, I not have...
<%@ page import="scsclass.*" %>
<%@ page import="java.util.ArrayLis
in my jsp. And, in my java source I have...
package scsclass;
No errors occur in my java source file now. But, I do get the following jsp error...
The import scsclass cannot be resolved.
What do you suggest now? Was my changing 'java' to 'scsclass' a good idea, or should I change back?
> What do you suggest now? Was my changing 'java' to 'scsclass' a good idea, or should I change back?
Yes, it will make your code less confusing...
> aren't I importing everything in Java's java subdirectory instead of my own java folder?
You would be importing bot, but as there aren't any classes in Suns "java.*" package, you would only be importing your own classes (but it would look confusing)
> The import scsclass cannot be resolved.
You have copied your classes to:
WEB-INF/classes/scsclass
yeah? Try restarting tomcat...
Yes, it will make your code less confusing...
> aren't I importing everything in Java's java subdirectory instead of my own java folder?
You would be importing bot, but as there aren't any classes in Suns "java.*" package, you would only be importing your own classes (but it would look confusing)
> The import scsclass cannot be resolved.
You have copied your classes to:
WEB-INF/classes/scsclass
yeah? Try restarting tomcat...
ASKER
After changes, I have my original set of reported errors back. To reduce miscommunication, here's my current setup and reported errors...
File structure...
src/scsclass <-- java source files
SCS <-- htm,jsp files
SCS/WEB-INF/classes/scscla ss <-- compiled class files here
Here's my jsp import... <%@ page import="scsclass.*" %>
And error... the import scsclass cannot be resolved
Here's my java package... package scsclass;
And error... the declared package does not match the expected package
I stopped Tomcat, stopped Eclipse, and then restarted Eclipse. That's when I get the above errors which are reported by Eclipse. I've never gotten to the point of starting a browser and using Tomcat server, and invoking index.jsp.
File structure...
src/scsclass <-- java source files
SCS <-- htm,jsp files
SCS/WEB-INF/classes/scscla
Here's my jsp import... <%@ page import="scsclass.*" %>
And error... the import scsclass cannot be resolved
Here's my java package... package scsclass;
And error... the declared package does not match the expected package
I stopped Tomcat, stopped Eclipse, and then restarted Eclipse. That's when I get the above errors which are reported by Eclipse. I've never gotten to the point of starting a browser and using Tomcat server, and invoking index.jsp.
ASKER
No comment from TimYates for awhile. Anyone else around that could help me? (I'll split points.)
Does it work when you just run tomcat (not in eclipse)?
Sounds like it could be an Eclipse classpath/packaging/projec t setup problem...
Sounds like it could be an Eclipse classpath/packaging/projec
Try to remove cache file of Tomcat
Looking for folder like this and remove all those files and sub directory
%TOMCAT_HOME%\work\Catalin a\localhos t
Looking for folder like this and remove all those files and sub directory
%TOMCAT_HOME%\work\Catalin
ASKER
Increasing points to 500, as I think that's fair.
TimYates suggested I try Tomcat. When I do, when I enter http://localhost:8080/SCS/index.jsp in my browser, I get a HTTP 500 error as follows ...
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperEx ception: Unable to compile class for JSP
An error occurred at line: 34 in the jsp file: /index.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Tomcat\work\Catalina\lo calhost\SC S\org\apac he\jsp\ind ex_jsp.jav a:6: package scsclass does not exist
import scsclass.*;
^
C:\Tomcat\work\Catalina\lo calhost\SC S\org\apac he\jsp\ind ex_jsp.jav a:74: cannot resolve symbol
symbol : class SCSDAL
location: class org.apache.jsp.index_jsp
SCSDAL dal=new SCSDAL("");
^
sompol_kiatkamolchai, I deleted the %TOMCAT_HOME%\work\Catalin a\localhos t files\SCS before trying Tomcat.
(I am quitting for tonight, and will read this problem for replies in the morning.)
TimYates suggested I try Tomcat. When I do, when I enter http://localhost:8080/SCS/index.jsp in my browser, I get a HTTP 500 error as follows ...
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperEx
An error occurred at line: 34 in the jsp file: /index.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Tomcat\work\Catalina\lo
import scsclass.*;
^
C:\Tomcat\work\Catalina\lo
symbol : class SCSDAL
location: class org.apache.jsp.index_jsp
SCSDAL dal=new SCSDAL("");
^
sompol_kiatkamolchai, I deleted the %TOMCAT_HOME%\work\Catalin
(I am quitting for tonight, and will read this problem for replies in the morning.)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And are you sure that
SCSDAL.java
has
package scsclass ;
at the top of it?
SCSDAL.java
has
package scsclass ;
at the top of it?
ASKER
Sompol... you were right; I had classes under WEB-INF/classes/ instead of WEB-INF/classes/scsclass.
(I had my output folder set wrong in the project parameters. Fixed now. Problems remain.)
TimYates... Yes, SCSDAL.java has 'package scsclass;' at the top.
----
I just discovered that I have a very basic problem... I can't compile java programs! I haven't had this problem before. So, I think my classpath has become corrupted. However, when I set the classpath the problem remains. (I'm not sure I understand classpath.) If you guys will help me thru this issue, I suspect my other issues will be resolved also. (But, should you think I'm sidetracking the troubleshooting process, let me know.)
Before describing my "basic problem", here some background that was asked for. On my project, I was using Eclipse and these java files were working fine. Then, I was told to switch to BEA Weblogic Workshop (WLW), which I did, after which the java classes worked also. Then, I was told to convert back to Eclipse. (Arrrgh!!!) So, I created a new Eclipse project and imported the resources into Eclipse. That's when the problems with previously running java classes occurred.
Here's my "basic problem"...
Here's the simplest ever java file, tmp.java, which I created in my C:\Eclipse Workspace\SCS\src\scsclass source folder...
package scsclass;
public class tmp
{
public static void main(String[] args)
{
}
}
It has a main, but does nothing. And, it seems to compile OK. But, when I try to run it, I get the following error.
C:\Eclipse Workspace\SCS\src\scsclass >java tmp
Exception in thread "main" java.lang.NoClassDefFoundE rror: tmp (wrong name: scsclass/tmp)
at java.lang.ClassLoader.defi neClass0(N ative Method)
at java.lang.ClassLoader.defi neClass(Un known Source)
at java.security.SecureClassL oader.defi neClass(Un known Source)
at java.net.URLClassLoader.de fineClass( Unknown Source)
at java.net.URLClassLoader.ac cess$100(U nknown Source)
at java.net.URLClassLoader$1. run(Unknow n Source)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(Un known Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at sun.misc.Launcher$AppClass Loader.loa dClass(Unk nown Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at java.lang.ClassLoader.load ClassInter nal(Unknow n Source)
Here's my classpath...
CLASSPATH=.;c:\UltraEdit;C :\JavaUtil ;c:\Tomcat \common\li b\servlet- api.jar
Which I reset to...
CLASSPATH=.;C:\Eclipse Workspace\SCS\webroot\WEB- INF\classe s\scsclass ;C:\Eclips e
Workspace\SCS\src;c:\Ultra Edit;C:\Ja vaUtil;c:\ Tomcat\com mon\lib\se rvlet-api. jar
After which I get the same error. What should my classpath be?
(I had my output folder set wrong in the project parameters. Fixed now. Problems remain.)
TimYates... Yes, SCSDAL.java has 'package scsclass;' at the top.
----
I just discovered that I have a very basic problem... I can't compile java programs! I haven't had this problem before. So, I think my classpath has become corrupted. However, when I set the classpath the problem remains. (I'm not sure I understand classpath.) If you guys will help me thru this issue, I suspect my other issues will be resolved also. (But, should you think I'm sidetracking the troubleshooting process, let me know.)
Before describing my "basic problem", here some background that was asked for. On my project, I was using Eclipse and these java files were working fine. Then, I was told to switch to BEA Weblogic Workshop (WLW), which I did, after which the java classes worked also. Then, I was told to convert back to Eclipse. (Arrrgh!!!) So, I created a new Eclipse project and imported the resources into Eclipse. That's when the problems with previously running java classes occurred.
Here's my "basic problem"...
Here's the simplest ever java file, tmp.java, which I created in my C:\Eclipse Workspace\SCS\src\scsclass
package scsclass;
public class tmp
{
public static void main(String[] args)
{
}
}
It has a main, but does nothing. And, it seems to compile OK. But, when I try to run it, I get the following error.
C:\Eclipse Workspace\SCS\src\scsclass
Exception in thread "main" java.lang.NoClassDefFoundE
at java.lang.ClassLoader.defi
at java.lang.ClassLoader.defi
at java.security.SecureClassL
at java.net.URLClassLoader.de
at java.net.URLClassLoader.ac
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
Here's my classpath...
CLASSPATH=.;c:\UltraEdit;C
Which I reset to...
CLASSPATH=.;C:\Eclipse Workspace\SCS\webroot\WEB-
Workspace\SCS\src;c:\Ultra
After which I get the same error. What should my classpath be?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The 'java scsclass.tmp' approach you suggested worked, and I could run tmp.class. But, I still have some compilation problems. TimYates, out of fairness to you, I think I should comprehensively review my java compilation environment before taking more of your time. Obviously, something is hosed down! I'll be back shortly.
ASKER
I'm closing this call down. My problem is not solved, but I'm confident it is related to my Java installation, classpath, etc. Thanks TimYates for helping me, and also sompol. (I think I wasted your time.) When I get my installation working, (and I've been sidetracked by other matters), if I still have problems I will start another call. Thank you...
Assuming your classes have:
package java ;
at the top of their source files...
if not, You may need to add it in...