JSP compilation error when using JSONObject [duplicate] - java
I get the error: "Only a type can be imported. XYZ resolves to a package."
Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The java.util.* import works fine.
<%# page import="java.util.*"%>
<%# page import="org.eresearch.knowledgeportal.model.Category"%>
<%# page import="org.eresearch.knowledgeportal.dao.CategoryDao"%>
<%
CategoryDao catDao = new CategoryDao();
ArrayList<Category> catList = catDao.selectCategory();
//
%>
Edit: the actual error is below:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 7 in the generated java file
Only a type can be imported. org.eresearch.knowledgeportal.model.Category resolves to a package
Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that:
<%# page import="java.util.*,x.y.Z"%>
And x.y.Z can't be found on the classpath (i.e. is not present under WEB-INF/classes nor in a JAR of WEB-INF/lib).
Double check that the WAR you deploy on Tomcat has the following structure:
my-webapp
|-- META-INF
| `-- MANIFEST.MF
|-- WEB-INF
| |-- classes
| | |-- x
| | | `-- y
| | | `-- Z.class
| | `-- another
| | `-- packagename
| | `-- AnotherClass.class
| |-- lib
| | |-- ajar.jar
| | |-- bjar.jar
| | `-- zjar.jar
| `-- web.xml
|-- a.jsp
|-- b.jsp
`-- index.jsp
Or that the JAR that bundles x.y.Z.class is present under WEB-INF/lib.
OK I just solved it. In the last import I added a ";" by copying other code examples. I guess it's the standard line ending that is required.
So
<%# page import="java.util.*" %>
<%# page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
<%# page import="org.eresearch.knowledgeportal.model.Category" %>
became
<%# page import="java.util.*" %>
<%# page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
<%# page import="org.eresearch.knowledgeportal.model.Category;" %>
If you spell the class name wrong or the class isn't on the classpath, the JSP processor will say it "resolves to a package" rather than that it doesn't exist. This was driving me crazy today as I kept not seeing a typo I'd made.
I got this error in Netbeans. As with most bizarre errors like this that appear out of nowhere, I resolve it by going to project properties, changing the Source/Binary Format (doesn't matter to what, just something different), and doing a clean and build.
I got it resolved by adding the jars in tomcat lib directory.
Without further details, it sounds like an error in the import declaration of a class. Check, if all import declarations either import all classes from a package or a single class:
import all.classes.from.package.*;
import only.one.type.named.MyClass;
Edit
OK, after the edit, looks like it's a jsp problem.
Edit 2
Here is another forum entry, the problem seems to have similarities and the victim solved it by reinstalling eclipse. I'd try that one first - installing a second instance of eclipse with only the most necessary plugins, a new workspace, the project imported into that clean workspace, and hope for the best...
Got this exception as well.
Environment: Mac with Eclipse running Tomcat from inside Eclipse using Servers view.
For any reason Eclipse does not copy classes folder to WEB-INF. After classes folder was manually copied, everything works fine.
Don't know, or it is Eclipse bug or I missed something.
I experienced this weird error too, after changing letter case in the name of a class. The file was not copied to a tomcat server as expected, I had to delete it manually and redeploy. Maybe because I use case insensitive operating system?
I know it's kinda too late to reply to this post but since I don't see any clear answer i'd do it anyway...
you might wanna check out the MANIFEST.MF in META-INF on your eclipse.
then you might need to add the path of your class files like..
Class-Path: WEB-INF/classes
generate .class file separate and paste it into relevant package into the workspace.
Refresh Project.
This typically happens when mixing (in the same jsp page) static jsp import:
<%#include file="...
with dynamic jsp import:
<jsp:include page="...
and your type has been already imported by the "static imported" jsp. When the same type needs to be used (and then imported) by the "dynamically imported" jsp -> this generate the Exception: "Only a type can be imported..."
I had a similar issue. In eclipse I compared my project with a sample project which is working fine (generated by a maven archetype). I found my project has missed 2 lines in /.classpath file. I copied those 2 lines and it fixed the issue. It seems even though I set build path in project preferences, eclipse has not updated accordingly for some reasons.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
...
</classpath>
My contribution: I got this error because I created a package named 3lp.
However, according to java spec, you are not allowed to name your package starts with a number.
I changed it to _3lp, now it works.
My bet is that you have a package called org.ivec.eresearch.knowledgeportal.model.category (small c) and are running on a non-case sensitive filesystem like Windows or Mac. It seems that the compiler gets confused when a class and package exist.
You can either renamed the class "Category" or the package "category" and this error will go away. Unfortunately I'm not sure if this is a Tomcat or ECJ bug.
If you are using Maven and packaging your Java classes as JAR, then make sure that JAR is up to date. Still assuming that JAR is in your classpath of course.
I resolved it by adding the jar file containing the imported classes into WEB-INF/Lib.
Are you attempting to import an overridden class like I was?
If so, your overridden class is in the wrong package, or simply non-existent.
Creating, or moving the class into the correct location (src/[package.package].[class]) could solve your problem.
To me it was a wrong deployment. Deployed properly, everything works (check my question for details).
So I had the same issue and just wanted to give my solution. Maybe someone faces the same problem.
I had properly configured my artifact but somehow intellij mixed something up and deleted the WEB-INF folder in one of my artifacts.
When I viewed the contents of my .war file every class was present in the correct folder structure. So I didn't think that anything was missing. But when I check the artifacts in Intellij and compared it to a working artifact then I realized, that the WEB-INF folder with the classes and libs were not present.
Add them to the artifact and it should work.
TLDR: Intellij deleted the WEB-INF Folder in my .war artifact. Just check if yours is missing aswell. (Project Strucutre -> Artifacts)
The tomcat folder structure has Webapps/ROOT/ inside where the WEB-INF folder has to be present. If you place your WEB-INF inside Webapps/ tomcat is not locating the class files and jars.
Just adding yet another way of achieving this weird error
To me what happened is that the classpath had a few jars from tomcat by using the TOMCAT_HOME variable.
I didn't have that variable set and never needed it in over a year working in my current job. But this specific project was expecting this variable in the classpath and Eclipse wouldn't tell me there was a classpath error going on.
I was getting the same error and nothing was wrong with my java files and packages. Later I noticed that the folder name WEB-INF was written like this "WEB_INF". So correcting just the folder name solved the issue
I had this same issue when running from Eclipse. To fix the issue I went into the Configure Build Path and removed the src folder from the build path. Then closed and reopened the project. Then I added the src folder to the build path. The src folder contained the java class I was trying to access.
You have to import something FROM the package, like a class, enum, or interfacee, like this:
import some.package.SomeClass;
or, import everything from the package (not recommended)
import some.package.*;
edit: maybe I didn't read close enough. Where is the package you're trying to import from located on the filesystem? Is it under WEB-INF/lib?
Are there more details? (Is this in a JSP as in the linked webpage?)
If so, you can always just use the fully qualified class name.
rather than:
import foo.bar.*;
Baz myBaz;
you can use
foo.bar.Baz myBaz;
I had the same error message and my way to deal with it is as follows:
First go check the file directory where your Tomcat is publishing your web application, e.g. D:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\myDatatable\WEB-INF\classes, in which we normally put our classes. If this is not the place where you put your classes, then you have to find out where by default it is by right click your web application root name-->Build Path-->Configure Build Bath...-->Then check the "Source" Tab and find out the field value of "Default output folder". This shall be the place where Tomcat put your classes.
You would see that XYZ class is not yet built. In order to build it, you could go to Menu "Project"-->"Clean..."-->Select your web application to clean.
After it's completed, try restart your tomcat server and go check the file directory again. Your class should be there. At least it works for me. Hope it helps.
Related
Just installed IntelliJ, and it cannot find or load my main class [duplicate]
I creating a program to work with databases and I am getting the following error when compiling in IntelliJ IDEA. Does anyone why this is happening and how I could solve it?
The error that you get occurs not on complilation, but when you try to run your application. It happens because Java was not able to find Table.class file inside db subdirectory of the project output directory (classpath). It can happen for multiple reasons: wrong main class selected in the run/debug configuration Table.java is excluded from compilation (by accident or intentionally because it contained errors and you wanted to skip it while working on other code) class not compiled because Build step is excluded from from Before launch steps in the Run/Debug configuration project is misconfigured and there is no Source root defined for the directory containing db subdirectory Table.java has incorrect package statement or is located/moved to a different package project path contains a colon : on Mac/Linux or semicolon ; on Windows, it's used to separate the classpath and will render the classpath invalid. See this thread for details. Note that Finder on Mac may display colons in the path as slashes. the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details. In project structure make sure you have the right Java version for compile. there is a known bug that sometimes a Java project created from the Command Line template doesn't work because .idea/modules.xml file references invalid module file named untitled104.iml. Fix the module name manually or create a project from scratch and don't use a template. on Windows "Beta: Use Unicode UTF-8 for worldwide language support" Region Setting is enabled. See IDEA-247837 for more details and workarounds. When IntelliJ IDEA is configured to store module dependencies in Eclipse format source root configuration is lost due to a known bug. Configure the module to use IntelliJ IDEA format dependencies as a workaround. In a properly configured project and with the correct run/debug configuration everything works just fine:
the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details. I must again emphasis the point CrazyCoder has here. The (Oracle) JVM used to throw a SecurityException when you tried to run a Jar-File containing broken signatures. This made sense from a "What's wrong"-Point of view. That is no longer the case. They are indeed throwing ClassNotFoundExceptions now - even if the class is right there in the file (no matter if it is in the default package/toplevel or way down in a nested package structure).
Here's what worked for me: I deleted .ide folder, .iml file. And all other auto generated files by intelliJ then restarted my ide and I was asked if I want to make my project run with maven that's it. Obviously I said yes :)
This is a known bug in the IntelliJ idea. To fix this I just deleted the .iml and the .idea and restart the IDE. It works for most of the cases Edit: The files will be in the project directories.
In my case the default console app template works only if the project folder path does not contain underscore (_) in it. Underscore brings the error Error: Could not find or load main class com.company.Main Caused by: java.lang.ClassNotFoundException: com.company.Main IntelliJ IDEA 2021.3.1 (Ultimate Edition) Build #IU-213.6461.79, built on December 28, 2021
If you've tried everything else that others have suggested (deleting .idea folder, rebuild, etc) there's another place to check, especially if you've built an artifact jar. When you first build an artifact jar, IntelliJ adds a folder: META-INF to src directory. in it is a single file: MANIFEST.MF which has info pointing to the Main-Class for Java to find. If you've refactored your project package, unfortunately IntelliJ does not update this file with the new changes. My MANIFEST.MF has the following correct content: Manifest-Version: 1.0 Main-Class: org.umoja4life.fatashibackend.MainKt Where "org.umoja4life.fatashibackend" is the package name, and "MainKt" is IntelliJ's constructed name for a (pseudo) "Main Class" because fun main() has been defined in file "main.kt" in the package directory. Newbies: btw, This will be confusing for you because there should be no actual "class Main {}" definition despite the error message stating there should be. Before I discovered this file and after trying everyone else's suggestions, I found it quickest to just have IntelliJ start a project (with correct package name!), initialize it with a trivial main.kt having: fun main() { println("hello world!") } run and test that; then, I added back in all my other files, rebuilt, ran, and tested it. Apparently IntelliJ has some secret state information stored somewhere which doesn't get correctly updated if your refactor your package name for an already running project and jar.
IntelliJ: Source root is not handled correctly
I have the following project structure: ModuleName (=Content Root) |---src | |---com | |---company | |---file1.java | |---file2.java |---test-src | |---com | |---company | |---test.java |--- .classpath |--- .project This was an Eclipse project initially, and I need to import it in IntelliJ. However, everytime I try to import it, IntelliJ is completely confused with the source root folder. Looking at the java source files, I get the following error in the package declaration: package com.company; Error: Package name 'com.company' does not correspond to the file path 'src/com.company' and Error: Package name 'com.company' does not correspond to the file path 'test-src/com.company' I correctly marked the 'src' and 'test-src' folders as source-roots in IntelliJ. However, it always thinks that these folders are part of the package hierarchy. I have this problem in 3 of about 30 different modules. All modules are structured the same, and I don't really see any reason why IntelliJ works for most of them, but fails for those three. How can I resolve this?
For completeness' sake - if anyone runs into the same problem with a similar setup, here is the solution: Another module (let's call it module B) had a dependency on the .jar file generated by module A. This normally works perfectly fine, and the required .jar can be found in module B's lib folder. The issue was that module B's .classpath file (which was used to import the modules into IntelliJ) included the following entry (I assume it was generated by eclipse at some earlier point): <classpathentry kind="lib" path="lib/ModuleA.jar" sourcepath="/ModuleA/" /> Now it seems like IntelliJ became extremely confused by this sourcepath entry (as it doesn't point to a valid location) and this somehow affected module A's packaging structure. What's interesting about this (and made it a huge pain to debug and find the actual issue) is that the error didn't manifest itself in module B where the problematic .classpath file was located (because IntelliJ doesn't show a warning that the sources imported from the .classpath file were invalid), but rather in the parent module A. Removing the sourcepath entry (or pointing it to a valid location, i.e.: lib/ModuleA.jar) fixed the problem.
Importing Java Class inside JSP file
How to import Java class inside JSP file? <%#page import="javaname.java"%> is not working in Eclipse Neon. Already defined full path still not working. We have an existing project that the java class is inside of WEB-INF/classes instead of src folder but when we try to do it on another project, we cannot import anymore using the same syntax (<%# page import="package.javaclass"%>) Java class: JSP:
Maybe it should be: <%#page import="package.nameOfTheYouClass"%> You don't need to add the .java ending to the class name.
The Totp.java source file doesn't belong in the WEB-INF/classes folder, it belongs in the fa folder under src so that Eclipse will compile it for you. At runtime the server is supposed to find the compiled Totp.class file there. I'm guessing that Totp.java is not actually in the source folder, meaning this was a correct error message all along. If you've been adding files to, and directly editing files in, the WEB-INF/classes folder, you're doing it wrong. That folder is only ever supposed to hold classes compiled from the source folders like src and other resources copied there, by Eclipse, from the source folders.
check if the name of your class is right and that's it.
In eclipse, is it possible to change what the default package is without changing the source folder?
I'm working with some people on a java project. Problem is, I'm the only one using eclipse. The source files are located in svn in trunk/src/*.java. However, if I import that as a project directory, the default package is "" instead of what the actual project package name is. Is there a way to change that without changing the source location and the package name? Thanks!
If you mean that you want code in package foo.bar without having a matching directory folder of foo/bar under some source root - no, I don't think Eclipse supports that. While the convention of source locations having to match package structure isn't enforced by the language specification, it's mentioned there and so widely respected that I think it would be a bad idea to do anything else.
Eclipse requires a directory structure that matches the package structure. There is no option to have some package prefix that isn't reflected in directories. IntelliJ can work with this, and it's what most people expect to see most of the time anway.
I think you are checking out the incorrect root folder. If you are trying to work with a collection of source files located under trunk/src/ you may be don't need to check out this folder, because you will loose your reference to the main package (for example foo.bar) because it will be the base package. You may need to check out the trunk/ folder, because Eclipse expects to find the source files under the default /src folder. Once you have your main root folder (with a lot of files like .project, .classpath inside), it is likely possible that Eclipse will recognize your folder structure and configuration and your project will compile without problems.
Java error: Only a type can be imported. XYZ resolves to a package
I get the error: "Only a type can be imported. XYZ resolves to a package." Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The java.util.* import works fine. <%# page import="java.util.*"%> <%# page import="org.eresearch.knowledgeportal.model.Category"%> <%# page import="org.eresearch.knowledgeportal.dao.CategoryDao"%> <% CategoryDao catDao = new CategoryDao(); ArrayList<Category> catList = catDao.selectCategory(); // %> Edit: the actual error is below: org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 7 in the generated java file Only a type can be imported. org.eresearch.knowledgeportal.model.Category resolves to a package
Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that: <%# page import="java.util.*,x.y.Z"%> And x.y.Z can't be found on the classpath (i.e. is not present under WEB-INF/classes nor in a JAR of WEB-INF/lib). Double check that the WAR you deploy on Tomcat has the following structure: my-webapp |-- META-INF | `-- MANIFEST.MF |-- WEB-INF | |-- classes | | |-- x | | | `-- y | | | `-- Z.class | | `-- another | | `-- packagename | | `-- AnotherClass.class | |-- lib | | |-- ajar.jar | | |-- bjar.jar | | `-- zjar.jar | `-- web.xml |-- a.jsp |-- b.jsp `-- index.jsp Or that the JAR that bundles x.y.Z.class is present under WEB-INF/lib.
OK I just solved it. In the last import I added a ";" by copying other code examples. I guess it's the standard line ending that is required. So <%# page import="java.util.*" %> <%# page import="org.eresearch.knowledgeportal.dao.CategoryDao" %> <%# page import="org.eresearch.knowledgeportal.model.Category" %> became <%# page import="java.util.*" %> <%# page import="org.eresearch.knowledgeportal.dao.CategoryDao" %> <%# page import="org.eresearch.knowledgeportal.model.Category;" %>
If you spell the class name wrong or the class isn't on the classpath, the JSP processor will say it "resolves to a package" rather than that it doesn't exist. This was driving me crazy today as I kept not seeing a typo I'd made.
I got this error in Netbeans. As with most bizarre errors like this that appear out of nowhere, I resolve it by going to project properties, changing the Source/Binary Format (doesn't matter to what, just something different), and doing a clean and build.
I got it resolved by adding the jars in tomcat lib directory.
Without further details, it sounds like an error in the import declaration of a class. Check, if all import declarations either import all classes from a package or a single class: import all.classes.from.package.*; import only.one.type.named.MyClass; Edit OK, after the edit, looks like it's a jsp problem. Edit 2 Here is another forum entry, the problem seems to have similarities and the victim solved it by reinstalling eclipse. I'd try that one first - installing a second instance of eclipse with only the most necessary plugins, a new workspace, the project imported into that clean workspace, and hope for the best...
Got this exception as well. Environment: Mac with Eclipse running Tomcat from inside Eclipse using Servers view. For any reason Eclipse does not copy classes folder to WEB-INF. After classes folder was manually copied, everything works fine. Don't know, or it is Eclipse bug or I missed something.
I experienced this weird error too, after changing letter case in the name of a class. The file was not copied to a tomcat server as expected, I had to delete it manually and redeploy. Maybe because I use case insensitive operating system?
I know it's kinda too late to reply to this post but since I don't see any clear answer i'd do it anyway... you might wanna check out the MANIFEST.MF in META-INF on your eclipse. then you might need to add the path of your class files like.. Class-Path: WEB-INF/classes
generate .class file separate and paste it into relevant package into the workspace. Refresh Project.
This typically happens when mixing (in the same jsp page) static jsp import: <%#include file="... with dynamic jsp import: <jsp:include page="... and your type has been already imported by the "static imported" jsp. When the same type needs to be used (and then imported) by the "dynamically imported" jsp -> this generate the Exception: "Only a type can be imported..."
I had a similar issue. In eclipse I compared my project with a sample project which is working fine (generated by a maven archetype). I found my project has missed 2 lines in /.classpath file. I copied those 2 lines and it fixed the issue. It seems even though I set build path in project preferences, eclipse has not updated accordingly for some reasons. <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/java"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/> ... </classpath>
My contribution: I got this error because I created a package named 3lp. However, according to java spec, you are not allowed to name your package starts with a number. I changed it to _3lp, now it works.
My bet is that you have a package called org.ivec.eresearch.knowledgeportal.model.category (small c) and are running on a non-case sensitive filesystem like Windows or Mac. It seems that the compiler gets confused when a class and package exist. You can either renamed the class "Category" or the package "category" and this error will go away. Unfortunately I'm not sure if this is a Tomcat or ECJ bug.
If you are using Maven and packaging your Java classes as JAR, then make sure that JAR is up to date. Still assuming that JAR is in your classpath of course.
I resolved it by adding the jar file containing the imported classes into WEB-INF/Lib.
Are you attempting to import an overridden class like I was? If so, your overridden class is in the wrong package, or simply non-existent. Creating, or moving the class into the correct location (src/[package.package].[class]) could solve your problem.
To me it was a wrong deployment. Deployed properly, everything works (check my question for details).
So I had the same issue and just wanted to give my solution. Maybe someone faces the same problem. I had properly configured my artifact but somehow intellij mixed something up and deleted the WEB-INF folder in one of my artifacts. When I viewed the contents of my .war file every class was present in the correct folder structure. So I didn't think that anything was missing. But when I check the artifacts in Intellij and compared it to a working artifact then I realized, that the WEB-INF folder with the classes and libs were not present. Add them to the artifact and it should work. TLDR: Intellij deleted the WEB-INF Folder in my .war artifact. Just check if yours is missing aswell. (Project Strucutre -> Artifacts)
The tomcat folder structure has Webapps/ROOT/ inside where the WEB-INF folder has to be present. If you place your WEB-INF inside Webapps/ tomcat is not locating the class files and jars.
Just adding yet another way of achieving this weird error To me what happened is that the classpath had a few jars from tomcat by using the TOMCAT_HOME variable. I didn't have that variable set and never needed it in over a year working in my current job. But this specific project was expecting this variable in the classpath and Eclipse wouldn't tell me there was a classpath error going on.
I was getting the same error and nothing was wrong with my java files and packages. Later I noticed that the folder name WEB-INF was written like this "WEB_INF". So correcting just the folder name solved the issue
I had this same issue when running from Eclipse. To fix the issue I went into the Configure Build Path and removed the src folder from the build path. Then closed and reopened the project. Then I added the src folder to the build path. The src folder contained the java class I was trying to access.
You have to import something FROM the package, like a class, enum, or interfacee, like this: import some.package.SomeClass; or, import everything from the package (not recommended) import some.package.*; edit: maybe I didn't read close enough. Where is the package you're trying to import from located on the filesystem? Is it under WEB-INF/lib?
Are there more details? (Is this in a JSP as in the linked webpage?) If so, you can always just use the fully qualified class name. rather than: import foo.bar.*; Baz myBaz; you can use foo.bar.Baz myBaz;
I had the same error message and my way to deal with it is as follows: First go check the file directory where your Tomcat is publishing your web application, e.g. D:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\myDatatable\WEB-INF\classes, in which we normally put our classes. If this is not the place where you put your classes, then you have to find out where by default it is by right click your web application root name-->Build Path-->Configure Build Bath...-->Then check the "Source" Tab and find out the field value of "Default output folder". This shall be the place where Tomcat put your classes. You would see that XYZ class is not yet built. In order to build it, you could go to Menu "Project"-->"Clean..."-->Select your web application to clean. After it's completed, try restart your tomcat server and go check the file directory again. Your class should be there. At least it works for me. Hope it helps.