Related
I have a java class in my Eclipse web-app project that generate a XML file. This xml file will be used to display its content in a JSP page through an AJAX call. Suppose I want to export the project and send it to another user who will then use it on another PC. How can I create the xml file in the project directory even though I don't know the path it will have on the new computer?
Most Java projects have a standardized project structure.
e.g. Maven looks like follows:
my-app
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- app
| | `-- App.java
| `-- resources
|
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
Files a Java application will create with a relative path will read from the resources folder when run locally (of course this can be configures in the runtime environment in Eclipse or tweaked in the java/javac CLI command).
Note a packaged Java program cannot write into the packaged JAR or WAR file. So when creating a JAR/WAR you can embed static content, but writing doesn't work. So when you run JSP pages classloading comes into play. Different uses get sandboxed to their own context on the server side. Therefore sharing the same file and/or disk space might not be a good idea in overall.
When you have a file DB or similar, you should have a server configuration variable to set the path to the value that's used on your server. The complete configuration in your JSP environment is handeled in the web.xml file:
https://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html
It's the standard way for J2EE containers by the way.
If you have a local project and not a fully blown production environment, the simplest options might be reading out an environment variable with the local path you want to use on your machine.
Note that if you want to write a file from your J2EE container (=the software that's running the JSP) there might be security rules needed you need to set in Tomcat/Glassfish/Wildfly/...
One way is to pass the path as a system property -Dproperty=value (e.g. java -DxmlDir="C:\xml" SomeClass). On Eclipse, you can add this in Run Configurations, then I think it was either in Arguments or Environment. To access the property, simply do System.getProperty("xmlDir"). On the new computer, they can specify where ever they want the XML file to reside.
I am trying to build a small weather client API that will fetch some weather from some website. In doing so, I would like to have a single entry point to my project (call it WeatherClient).
I have separated the project into different packages (for simplicity lets just say I have 2 packages, an "external" package, and an "internal" package).
I only want my WeatherClient in the external package, and have a public class in the internal package (WeatherClientBuilderImpl with public builder()).
What I am trying to achieve is to hide the WeatherClientBuilderImpl when importing the project. From what I have read on modules it seems like this is exactly one of their primary benefits, yet I have sadly been unable to find anything about how to achieve this
I have tried a few things:
I have tried creating 2 modules: internal, and external. I have tried to just import the internal .jar (whose pom includes the dependency for the module) - The main issue I face here is getting a ClassNotFound exception at runtime.
I have tried creating 1 module and only exporting the external package. This doesn't work (I'm guessing because "export" exports the module and not the packages). Here I end up being able to implement any of the classes, which defeats my purpose.
This is my current tree and more or less what I would have liked to have (I tried doing some stuff with modules by placing my WeatherClient in an "external" package and then creating 2 modules (one for external, one for internal).
.
|-- WeatherClient.java
|-- internal
| |-- DarkKey.java
| |-- DarkSkyClient.java
| |-- InvalidParametersException.java
| |-- WeatherBuilderImpl.java
| `-- handlers
| |-- JsonBodyHandler.java
| `-- JsonBodyHandlerTwo.java
`-- model
|-- DateTimeFormatException.java
|-- DateValidator.java
`-- WeatherRequest.java
To be clear, what I'm trying to achieve is to import this project into a separate project, in such a way that a client can only be created by using the WeatherClient class.
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.
I have inherited a webapp built using NetBean's internal ant.
All jsps reside in:
WEB-INF/jsp
And the web.xml has hardcoded links to /WEB-INF/jsp/somefile.jsp
How can I use the maven war plugin to place the JSP there, maintaining consistency with the current structure ?
My pom currently reads:
<warSourceDirectory>${basedir}/web/WEB-INF</warSourceDirectory>
What the problem? Let's look at a standard war project structure:
$ mvn archetype:create -DgroupId=com.mycompany.app \
> -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
...
$ tree my-webapp/
my-webapp/
|-- pom.xml
`-- src
`-- main
|-- resources
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp
5 directories, 3 files
No need to configure anything, just put your JSPs under src/main/webapp/WEB-INF/jsp and there you go.
EDIT: I don't understand why you have the following line in your maven-war-plugin configuration:
<warSourceDirectory>${basedir}/web/WEB-INF</warSourceDirectory>
What is the expected behavior? Why don't you use the default value ${basedir}/src/main/webapp?
Also note that everything in src/main/resources will be "on the classpath", i.e., it all is copied to my-webapp/WEB-INF/classes when the war is built. So that's a good place to put your configuration files, for example, log4j.xml / logback.xml, or Spring's applicationContext.xml and Spring's other config files, which you can then easily reference with classpath:somefile.xml.
What also makes this very nice is you can set up filters in maven so that it transforms the files from src/resources before it puts them in the war file.
So if you have any config files, other than web.xml, in src/main/webapp/WEB-INF, think about moving them to the src/main/resources directory.
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.