Why FileWriter doesn't create a new file? - java

Consider the following code:
m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat"));
m_Writer.println(Integer.toString(s_NumOfLogins));
m_Writer.println(m_LoginHistoryStr);
m_Writer.close();
Any ideas why I don't find any file called LoginHistory.dat?
Thanks
Update: I've just found that I get an exception after line:
m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat"));
and its details are:
Any ideas what is the real problem?
Listening for transport dt_shmem at address: tomcat_shared_memory_id
27/05/2012 15:52:17 org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/SignInAndGetIp]
27/05/2012 15:52:17 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor SignInAndGetIp.xml from C:\Users\user\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
27/05/2012 15:52:17 org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SignInAndGetIp]] after start() had already been called. The second call will be ignored.
java.io.FileNotFoundException: LoginHistory.dat (‏‏Access denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at signIn.SignInAndShowIPTableServlet.init(SignInAndShowIPTableServlet.java:60)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1228)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1147)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:836)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

It seems plain that you don't have permission to create that file where you're trying to create it, hence the "Access denied" message. You'd need to find a directory where you're allowed to create files. If you're not sure where a file is going to be created, you can see its full path with File.getAbsolutePath() or File.getCanonicalPath().

Related

dynamic web project: "java.lang.ClassNotFoundException" despite .jar file in the WEB-INF/lib directory

I'm working on an assignment that requires some .jar files. The assignment requires me to do some work on a Tomcat servlet with JSP files.
I included my .jars in the Java Build Path in Eclipse, and copied/added them to the WEB-INF/lib directory.
When I run the project like so: http://localhost:8080/project/ I see the index.jsp page with empty divs where data should be from the code executed via the TestServlet.java.
Looking for the reason why, I see the following error when I check out http://localhost:8080/project/TestServlet:
May 3, 2013 1:22:26 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [TestServlet] in context with path [/project] threw exception [Servlet execution threw an exception]
with root cause java.lang.ClassNotFoundException: com.aetrion.flickr.FlickrException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
at TestServlet.doGet(TestServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
Which is weird, since, again, I included the .jars in the Java Build Path and copied/added the .jars to the WEB-INF/lib directory.
Any ideas or help?
just to be sure, first you put the jar in the web-inf/lib and after you add it in t
project > build path > configure build path > add jar , in that order , correct?
try in the "libraries" tab moving the jar at the top or almost the top... would be a problem of visibility
You must try to add the jar in your classpath, project > build path > configure build path > add jar (or external jar)
Even i had the same problem.This is how I solved it:
You can add the external jar to the tomcat folder->lib-> add your external jar in your system ,after adding remove that folder from your project and add again or you can remove the tomcat folder and copy all the jars from tomcat folder->lib and you can add it as your external jars to your project

TomEE DataSource isn't found

I have a strange problem regarding TomEE and using a DataSource specified in tomee.xml.
It might be worth noting that I'm using Netbeans, TomEE and MySQL. Running on Ubuntu 13.04 (Xubuntu latest)
The tomee.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="booktablesDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql:localhost:3306/book_tables
UserName juser
Password jpassword
JtaManaged true
</Resource>
</tomee>
And a small codesnippet, testing the connection pool:
#Resource DataSource booktablesDS;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Connection c = booktablesDS.getConnection();
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM BOOKS");
The output I'm getting from TomEE is:
SEVERE: null
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
Full stacktrace follows.
May 06, 2013 11:08:53 AM org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/EJBTest]] after start() had already been called. The second call will be ignored.
May 06, 2013 11:08:54 AM MainS processRequest
SEVERE: null
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at MainS.processRequest(MainS.java:35)
at MainS.doGet(MainS.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:273)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 22 more
May 06, 2013 11:08:55 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/EJBTest]
Basicly it feels like netbeans is deploying the project with another tomee.xml file? Disregarding the values in my tomee.xml, which is under tom-ee/conf/tomee.xml
Help would be awesome.
Looks like your url is wrong.
jdbc:mysql:localhost:3306/book_tables
should be
jdbc:mysql://localhost:3306/book_tables
Can you double-check whether the MySQL driver JAR is really there in tomee/lib folder.
Try to change your code like this:
#Resource(name = "booktablesDS", type = javax.sql.DataSource.class)
DataSource booktablesDS;

Maven Eclipse Tomcat ClassNotFoundException

I created web App Test Project using the following Maven command:
mvn archetype:generate -DgroupId=com.vanilla.cache -DartifactId=TestProject -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
After it I converted this project to Eclipse
mvn eclipse:eclipse -Dwtpversion=2.0
After it I created new Servet named com.vanilla.test.TestServlet in eclipse and when I'm running it using Tomcat 7.0. I'm getting the following error.
SEVERE: Allocate exception for servlet Test
java.lang.ClassNotFoundException: com.vanilla.test.TestServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:836)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
Please help. What am I missing?
I solved the problem.
Project - > Properties -> Java Build Path -> Source .
I added /src and restarted tomcat.

Exception while trying to use jersey and jaxb

The following code:
#POST
#Path("/previous-status/{current}")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.TEXT_PLAIN)
public String getPreviousStepStatus(#PathParam("current") JAXBElement<WorkflowStep> step) {
WorkflowStep wfStep = step.getValue();
return DBAccessor.getPrevStepStatus(wfStep);
}
Produces the following exception:
javax.servlet.ServletException: Servlet.init() for servlet Jersey Rest Service threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
root cause
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
If it is commented I don't get the exception. The currently used libraries in the web application are:
asm-3.1.jar
jersey-core-1.11.jar
jersey-server-1.11.jar
jsr311-api-1.1.1.jar
jersey-servlet-1.11.jar
activation.jar (part of jaxb distribution)
jaxb-api.jar
jaxb-impl.jar
stax-api-1.0.1.jar
woodstox-core-asl-4.1.2.jar
The reason I have included the last 5 libraries in the list is this article from developer works
Furthermore from the tomcat start up log I see the following:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String restful.SilverLine.getPreviousStepStatus(javax.xml.bind.JAXBElement) at parameter at index 0
SEVERE: Method, public java.lang.String restful.SilverLine.getPreviousStepStatus(javax.xml.bind.JAXBElement), annotated with POST of resource, class restful.SilverLine, is not recognized as valid resource method.
Any ideas will be appreciated?
PathParameter and entity is something different, try following:
#POST
#Path("/previous-status/{order}")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.TEXT_PLAIN)
public String getPreviousStepStatus(#PathParam("order") int order, JAXBElement<WorkflowStep> step) {
...
WorkflowStep wfStep = step.getValue();
return DBAccessor.getPrevStepStatus(wfStep);
}
in this case, you can do requests like POST http://host/previous-status/10 and put whatever you want into entity.
You had xml in path param (which is part of URL), which is not possible to use. Your request would look like POST http://host/previous-status/<some-xml><foo>bar</foo></some-xml > , which is .. not good idea and not supported.
You should take a look at Jersey user guide: http://jersey.java.net/nonav/documentation/latest/ , topics like path param injection, accessing entity and working with XML are well covered there.

Error while deploying java webservice

Could someone help me on this.
I have created simple java web service using Java 1.6, tomcat-6.0.20 and axis2-1.4.1.
While deploying the application, it returns following error message:
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 22 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
Feb 25, 2010 11:58:37 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
Seems that there is an incompatibility of libraries loaded.
See this post and this
The first post explain the error:
In Tomcat 6, the JSP API classes are found in jsp-api.jar and servlet
API classes are found in servlet-api.jar, with both located in the lib
directory of the Tomcat 6 installation. Any servlet.jar you find
involved with this server would contain out of date classes for this
server.
So... check if you don't have an extra servlet.jar:
Make sure you don't
have a copy of servlet.jar in the WEB-INF/lib of your webapp or
somewhere else involved with the running server, such as in the
lib/ext directory of your JDK or JRE.
I hope it helps you.

Categories

Resources