java.lang.NoClassDefFoundError when referencing an external library in Eclipse - java

Edit:
I found the answer and I am posting it here in case anybody googles it in future: Turns out that Tomcat does not look up external libraries in Eclipse and they should be copied to [TomcatInstallDir]/lib.
Here is my code (which is for a servlet running on Tomcat server):
import com.google.gson.Gson;
public class AddCourse extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
Gson gson = new Gson();
//Some code
}
I have tried to export other libraries as well as same library with different version and had the same problem. Here is a picture of Build path:
I read that when writing a Dynamic Web Application (Which I am doing) then we should put the jars under Web-Content/lib, I did that also and it did not help.
Here is the stacktrace:
Apr 19, 2020 5:27:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AddCourse] in context with path [/MTHDB] threw exception [Servlet execution threw an exception] with root cause
java.lang.Error: Unresolved compilation problems:
Gson cannot be resolved to a type
Gson cannot be resolved to a type
at servlets.AddCourse.doPost(AddCourse.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Edit:
I noticed that my MANIFEST.MF' contents looks like this:
Manifest-Version: 1.0
Class-Path:
Should not there be a mention the the JARs?

I found the answer and I am posting it here in case anybody googles it in future: Turns out that Tomcat does not look up external libraries in Eclipse and they should be copied to [TomcatInstallDir]/lib.

Related

Genexus QueryViewer

I have the following runtime error. This happens when I run a web panel that contains a queryviewer.
The project is deployed on a Tomcat 8 server.
The database is SQLServer 2011
Genexus 16 (Upgrade 11)
Error: java.sql.SQLException: Invalid SQL statement or JDBC escape, terminating '"' not found. at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:1155) at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:156) at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.(JtdsPreparedStatement.java:104) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2221) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2179) at com.genexus.db.driver.GXConnection.prepareStatement(GXConnection.java:882) at com.genexus.db.driver.PreparedStatementCache.createStatement(PreparedStatementCache.java:77) at com.genexus.db.driver.PreparedStatementCache.getStatement(PreparedStatementCache.java:120) at com.genexus.db.driver.GXConnection.getStatement(GXConnection.java:789) at com.genexus.db.driver.GXConnection.getStatement(GXConnection.java:801) at com.genexus.db.SentenceProvider.getPreparedStatement(SentenceProvider.java:16) at com.genexus.db.ForEachCursor.preExecute(ForEachCursor.java:159) at com.genexus.db.DataStoreProvider.execute(DataStoreProvider.java:233) at com.genexus.db.DataStoreProvider.execute(DataStoreProvider.java:204) at qviewer.services.gxpl_DBAccess.ExecuteSQL(gxpl_DBAccess.java:95) at qviewer.services.gxpl_DBAccess.ExecuteSQL(gxpl_DBAccess.java:225) at qviewer.services.Sdtgxpl_DBAccess.executesql(Sdtgxpl_DBAccess.java:67) at qviewer.services.data.recordset.gxpl_getrecordsetquery.privateExecute(gxpl_getrecordsetquery.java:101) at qviewer.services.data.recordset.gxpl_getrecordsetquery.execute_int(gxpl_getrecordsetquery.java:75) at qviewer.services.data.recordset.gxpl_getrecordsetquery.execute(gxpl_getrecordsetquery.java:55) at qviewer.services.data.recordset.gxpl_getrecordset.privateExecute(gxpl_getrecordset.java:166) at qviewer.services.data.recordset.gxpl_getrecordset.execute_int(gxpl_getrecordset.java:112) at qviewer.services.data.recordset.gxpl_getrecordset.execute(gxpl_getrecordset.java:77) at qviewer.services.gxpl_getrecordsetpagedata.privateExecute(gxpl_getrecordsetpagedata.java:179) at qviewer.services.gxpl_getrecordsetpagedata.execute_int(gxpl_getrecordsetpagedata.java:162) at qviewer.services.gxpl_getrecordsetpagedata.execute(gxpl_getrecordsetpagedata.java:107) at qviewer.services.gxpl_getpagedatafortable_logic.privateExecute(gxpl_getpagedatafortable_logic.java:105) at qviewer.services.gxpl_getpagedatafortable_logic.execute_int(gxpl_getpagedatafortable_logic.java:83) at qviewer.services.gxpl_getpagedatafortable_logic.execute(gxpl_getpagedatafortable_logic.java:60) at qviewer.services.gxpl_getpagedatafortable.privateExecute(gxpl_getpagedatafortable.java:71) at qviewer.services.gxpl_getpagedatafortable.execute_int(gxpl_getpagedatafortable.java:52) at qviewer.services.gxpl_getpagedatafortable.execute(gxpl_getpagedatafortable.java:35) at qviewer.services.gxpl_get_main.privateExecute(gxpl_get_main.java:88) at qviewer.services.gxpl_get_main.execute_int(gxpl_get_main.java:52) at qviewer.services.gxpl_get_main.execute(gxpl_get_main.java:35) at qviewer.services.agxpl_get_impl.privateExecute(agxpl_get_impl.java:53) at qviewer.services.agxpl_get_impl.webExecute(agxpl_get_impl.java:43) at com.genexus.webpanels.GXWebObjectBase.doExecute(GXWebObjectBase.java:218) at qviewer.services.agxpl_get.doExecute(agxpl_get.java:22) at com.genexus.webpanels.GXWebObjectStub.callDoExecute(GXWebObjectStub.java:242) at com.genexus.webpanels.GXWebObjectStub.callExecute(GXWebObjectStub.java:124) at com.genexus.webpanels.GXWebObjectStub.doPost(GXWebObjectStub.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at com.genexus.filters.ExpiresFilter.doFilter(ExpiresFilter.java:1241) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
I had the exact same problem with a Linux implementation, don't know if it's your case but here is what I found: it turns that the XML files "com.app.QueryViewerObjects.xml", "com.app.QueryViewerQueries.xml" and "com.app.QueryViewerSecurity.xml" had the BOM mark. Once removed the mark, the QV worked like a charm.
There could be another reason for this error and has to do with the lifetime of the KB. Sometimes an upgrade or a new version of Genexus comes with new versions of JARs, and sometimes comes with the exact same JAR BUT with the names with uppercases instead of lowercases as previous versions. In Windows there is no problem because the JAR are overwritten, but in Linux nope. So you've end with two JARs in the WEB-INF/lib folder of two diferents versions. It happend to me with following the QueryViewers JARs:
QueryViewerServicesCommon.jar
QueryviewerServicesCommon.jar
and
QueryViewerServicesRuntime.jar
QueryviewerServicesRuntime.jar
That "v" uppercase/lowercase in Linux makes the files differents.

How do I fix SEVERE: Allocate exception for servlet after Project clean?

I am working on a dynamic web project using JSP and multiple servlets with tomcat Apache and Eclipse.
I was having multiple problems with Eclipse today and I ran a clean on the project to try to fix some of the issues that I was having and ever since, I'm getting the following error every time a servlet is called.
SEVERE: Allocate exception for servlet [RegisterServlet]
java.lang.ClassNotFoundException: com.ogi.controller.RegisterServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1372)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1195)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:545)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:526)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1044)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:763)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1629)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
This project was working completely fine on localhost before the clean and I have all of the servlets defined in my web.xml file. CATALINA has an environmental variable and I already have the Jar files in my project.
I'm not sure why these errors are occurring and would appreciate some direction on how to attempt to fix this.

Can't initialize GraphQL: wrong ANTLR Tool runtime version

My application fails at the first query trying to initialize GraphQL with this error:
ANTLR Tool version 4.7.2 used for code generation does not match the current runtime version 4.5.1
I don't explicitely use this library in my project, I tried to add a newer version to the pom nothing changed.
Original issue: https://github.com/graphql-java-kickstart/graphql-java-servlet/issues/241
Full stack trace:
ANTLR Tool version 4.7.2 used for code generation does not match the current runtime version 4.5.1ANTLR Runtime version 4.7.2 used for parser compilation does not match the current runtime version 4.5.1Apr 06, 2020 4:54:41 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
java.lang.ExceptionInInitializerError
at graphql.parser.Parser.parseDocument(Parser.java:57)
at graphql.parser.Parser.parseDocument(Parser.java:39)
at graphql.kickstart.tools.SchemaParserBuilder.parseDocuments(SchemaParserBuilder.kt:181)
at graphql.kickstart.tools.SchemaParserBuilder.parseDefinitions(SchemaParserBuilder.kt:169)
at graphql.kickstart.tools.SchemaParserBuilder.scan(SchemaParserBuilder.kt:162)
at graphql.kickstart.tools.SchemaParserBuilder.build(SchemaParserBuilder.kt:207)
at it.cims30.core.api.graphql.GraphQLEndpoint.createSchema(GraphQLEndpoint.java:23)
at it.cims30.core.api.graphql.GraphQLEndpoint.getConfiguration(GraphQLEndpoint.java:17)
at graphql.kickstart.servlet.AbstractGraphQLHttpServlet.init(AbstractGraphQLHttpServlet.java:85)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1124)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1079)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:761)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2300)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with UUID 59627784-3be5-417a-b9eb-8131a7286089 (expected aadb8d7e-aeef-4415-ad2b-8204d6cf042e or a legacy UUID).
at org.antlr.v4.runtime.atn.ATNDeserializer.deserialize(ATNDeserializer.java:153)
at graphql.parser.antlr.GraphqlLexer.<clinit>(GraphqlLexer.java:276)
... 30 more
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with UUID 59627784-3be5-417a-b9eb-8131a7286089 (expected aadb8d7e-aeef-4415-ad2b-8204d6cf042e or a legacy UUID).
... 32 more
Apr 06, 2020 4:54:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [it.cims30.core.api.graphql.GraphQLEndpoint]
java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with UUID 59627784-3be5-417a-b9eb-8131a7286089 (expected aadb8d7e-aeef-4415-ad2b-8204d6cf042e or a legacy UUID).
at org.antlr.v4.runtime.atn.ATNDeserializer.deserialize(ATNDeserializer.java:153)
at graphql.parser.antlr.GraphqlLexer.<clinit>(GraphqlLexer.java:276)
at graphql.parser.Parser.parseDocument(Parser.java:57)
at graphql.parser.Parser.parseDocument(Parser.java:39)
at graphql.kickstart.tools.SchemaParserBuilder.parseDocuments(SchemaParserBuilder.kt:181)
at graphql.kickstart.tools.SchemaParserBuilder.parseDefinitions(SchemaParserBuilder.kt:169)
at graphql.kickstart.tools.SchemaParserBuilder.scan(SchemaParserBuilder.kt:162)
at graphql.kickstart.tools.SchemaParserBuilder.build(SchemaParserBuilder.kt:207)
at it.cims30.core.api.graphql.GraphQLEndpoint.createSchema(GraphQLEndpoint.java:23)
at it.cims30.core.api.graphql.GraphQLEndpoint.getConfiguration(GraphQLEndpoint.java:17)
at graphql.kickstart.servlet.AbstractGraphQLHttpServlet.init(AbstractGraphQLHttpServlet.java:85)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1124)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1079)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:761)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2300)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
The current solution is (from the closed issue):
Revert graphql-java-tools to 5.7.1 and graphql-java to 13.0
The current version isn't compatible with graphql-java 14.0.
I also needed to add to my pom this:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
The graphql-java-tools I reverted to used a different version of commons-lang3 that gave a NullPointerException if the JDK version was too new (I'm using 13).

Getting An AccessDeniedException with Tomcat?

I want to have access to static resources outside my webapp so that they don't get destroyed every time I redeploy, but I can't seem to get them
I have a servlet that is meant to serve these images via a FileOutputStream, but it can't get the file when I deploy it on my tomcat server - it gives me a java.nio.file.AccessDeniedException.
Error from Catalina Log File
13-Feb-2020 16:53:10.621 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [fileGetter] in context with path [] threw exception
java.nio.file.AccessDeniedException: C:\Users\micro\Pictures\WebsiteStuff\BlogPosts\Test Image 2020-02-12.jpg
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:235)
at java.base/java.nio.file.Files.newByteChannel(Files.java:374)
at java.base/java.nio.file.Files.newByteChannel(Files.java:425)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
at java.base/java.nio.file.Files.newInputStream(Files.java:159)
at java.base/java.nio.file.Files.copy(Files.java:3195)
at ImageGetterServlet.doGet(ImageGetterServlet.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
This is especially strange given that I have allowed permission to the directory in my Catalina.policy file.
Catalina.Policy
grant {
...
permission java.io.FilePermission "C:${file.separator}Users${file.separator}micro${file.separator}Pictures${file.separator}WebsiteStuff", "read, write";
};
For whatever reason, this exact same code (minus the permission) works perfectly in Intellij. However, here it is not able to access the file.
Could anyone let me know why this is happening? Did I set the wrong permission/put the permission in the wrong place?
EDIT: It is NOT a permission problem
I granted everything in Catalina.policy java.security.AllPermission but to no avail, I’m getting the same exception.
I found out what was wrong. Tomcat cannot access anything inside the Users folder - probably a Windows 10 specific problem.
I just put my directory in question in the C: drive so that the path is now C:/WebsiteStuff and everything worked fine.

ClassNotFoundException - How to use external libraries without maven?

As a result of my previous question I followed these steps to include Apache PDFbox without maven:
downloaded .jars from download site of PDFbox (all under Libraries of each subproject)
downloaded .jars from dependencies of PDFbox from maven repository (commons-logging and fontbox, but fontbox was already included in step 1)
placed the downloaded .jars in a folder (C:\PDFjars)
configured build path in eclispe (added all .jars as external libraries) It now looks like this:
My servlet:
package servlets;
// other imports
import org.apache.pdfbox.pdmodel.PDDocument;
#WebServlet("/print")
public class PrintServlet extends HttpServlet {
public PrintServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// From a PDFbox Tutorial
//Creating PDF document object
PDDocument document = new PDDocument();
//Saving the document
document.save("C:/PdfBox_Examples/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
doGet(request, response);
}
}
So when I start my web project and call the function the should produce a simple PDF document, I get folloing error:
Servlet.service() for servlet [servlets.PrintServlet] in context with path [/StaticsCalculator] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at servlets.PrintServlet.doPost(PrintServlet.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Solution: I had to copy the .jars in WEB-INF/lib folder as well.
Download the jar file by itself and add it to your code dependency. You can search for any Maven file on Maven Repository. If you're using Eclipse, just modify the build path to point to an external jar and give it the location of your jar. This will not take care of any dependencies your library requires, which leads into dependency hell where you'll have to read the pom and download each library manually.
You can
a) use a local maven repository and place the dependencies there.
b) add the dependencies in the classpath of the application - that's basically what maven does for you.

Categories