FileNotFoundException while trying to write an image using thread in java - java

I am trying to write an image using ImageIO.write() inside run() method of a thread in java. But it is throwing FileNotFoundException and not allowing me to write the image. It is showing stack trace as
java.io.FileNotFoundException: C:\abc\pqr\abc.jpg (Access is denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at answerdetection.AnswerDetection.getId(AnswerDetection.java:286)
at getResult.Threader.run(Threader.java:95)
at java.lang.Thread.run(Unknown Source)
Exception in thread "t1" java.lang.NullPointerException
at javax.imageio.ImageIO.write(Unknown Source)
at answerdetection.AnswerDetection.getId(AnswerDetection.java:286)
at getResult.Threader.run(Threader.java:95)
at java.lang.Thread.run(Unknown Source)
Please suggest some solution. Thank You.

Run the Command Prompt or whatever IDE you are using as an Administrator and execute the program again. Else, change the path to a place when you have appropriate writing permissions.

It looks ike you have not the permission to write the file in C:\abc\pqr\

Related

Error connecting Java to SQL Anywhere database

I am trying to connect a java program to a database. I have sajdbc4.jar in the build path and it worked before, but now I keep getting this error when I try to make the connection:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no dbjdbc12 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at sybase.jdbc4.sqlanywhere.IDriver.try_load(IDriver.java:455)
at sybase.jdbc4.sqlanywhere.IDriver.(IDriver.java:396)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at java.util.ServiceLoader$LazyIterator.nextService(Unknown Source)
at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
at java.util.ServiceLoader$1.next(Unknown Source)
at java.sql.DriverManager$2.run(Unknown Source)
at java.sql.DriverManager$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.sql.DriverManager.loadInitialDrivers(Unknown Source)
at java.sql.DriverManager.(Unknown Source)
at Main.main(Main.java:26)
Can someome please help me? Can't find anything abount this issue online.
For windows find this file in your computer:
dbjdbc12.dll
For linux find this file:
libdbjdbc12.so
Put the location of this file on the java.library.path either with a command line option:
java -Djava.library.path=DIRECTORYWITHDLL ...
or using System.setProperty in your code:
System.setProperty("java.library.path","DIRECTORYWITHDLL");
For Linux, you need to add the dbjdbc12.so to java.library.path using:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/path/that/contain/dbjdbc12.so
this is the same for dbjdbc16 also.
Since, sybase.jdbc4 driver need this file. Please refer- https://blogs.sap.com/2014/05/02/connecting-to-sql-anywhere-using-jdbc-2/#:~:text=jar%20is%20in%20the%20classpath,Connection%20con%20%3D%20DriverManager.
Similarly, for windows add dbjdbc12.dll file.
For SQL Anywhere 17 libdbjdbc17.so was not suffiecient, it was then complaning about the next file:
failed: java.lang.UnsatisfiedLinkError: /usr/local/tomcat/native-jni-lib/libdbjdbc17.so: libdbtasks17_r.so: cannot open shared object file: No such file or directory
So I ended up adding all files from the lib64 folder from the Database client (after I installed it on some other linux machine) to my tomcat lib path and it worked.

NoSuchFileException when running jar file

When I run a jar file that accesses other files in the src directory, I get the following RuntimeException:
Exception in thread "main" java.nio.file.NoSuchFileException:
src\FileToBeAccessed.txt.lck
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newFileChannel(Unknown Source)
at java.nio.channels.FileChannel.open(Unknown Source)
at java.nio.channels.FileChannel.open(Unknown Source)
at java.util.logging.FileHandler.openFiles(Unknown Source)
at java.util.logging.FileHandler.(Unknown Source)
at Program.main(Program.java:30)
The code works fine when I run it directly without building a jar file.
You'll need to change how you access resources in a jar to a stream.
this.getClass().getResourceAsStream("/file.txt");
The correct way is
this.getClass().getResourceAsStream("file.txt");
It is really strange that
this.getClass().getResource("file.txt")
does work in IDE but NOT when you run it from a jar.
this.getClass().getResourceAsStream("file.txt")
works both inside IDE and from jar.

Unable to read pdf file in Marathi text in Java program

I am getting this exception in thread "AWT-EventQueue-0"
java.lang.NoClassDefFoundError: org/bouncycastle/asn1/DEREncodable
at com.lowagie.text.pdf.PdfEncryption.(Unknown Source)
at com.lowagie.text.pdf.PdfReader.readDecryptedDocObj(Unknown Source)
at com.lowagie.text.pdf.PdfReader.readDocObj(Unknown Source)
at com.lowagie.text.pdf.PdfReader.readPdf(Unknown Source)
at com.lowagie.text.pdf.PdfReader.(Unknown Source)
at com.lowagie.text.pdf.PdfReader.(Unknown Source
It looks like you're following the Getting started with iText Guide. I daresay you will find the answer to this question very useful.

Ignored exception: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException in Java Applet

I created a Java Applet which manipulates a JSP webpage's form fields. The JSP calls the Java Applet, of course.
This the error I'm getting in the Java Console, when I run my Java Applet on my JSP:
Ignored exception: java.security.PrivilegedActionException:
java.lang.reflect.InvocationTargetException
I am not calling InvocationTargetException or passing any exceptions in the code. Somehow, I managed to trace it, by painstaking trial-and-error, that the line causing the error is:
BlowfishCBC bfcbc = new BlowfishCBC(testkey, 0, testkey.length, CBCIV_START);
As a test, I disabled all code that has dependencies on the JSP, so I can just run it inside Eclipse. I created a main method to execute the Java Applet and then ran it inside Eclipse. I got no errors or warnings, at all.
I exported the same Java Applet, as a JAR, copied it to the server, restarted Apache Tomcat, went to the page on my web browser, ran the applet, and the same InvocationTargetException error occurred
What could be the problem?
Note: I am not using try..catch anywhere in my code. What can I put in the code that will capture this exception information and how do I do that?
Thank you.
Update:
Here is the stacktrace from the Java Console:
java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.checkLiveConnectCaller(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.access$000(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.checkLiveConnectCaller(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.access$000(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.checkLiveConnectCaller(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.access$000(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
in encrypt method
trying to get pwd value from document
got pwd from document, password is 12345
is this getting here?
Ignored exception: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
I think that's the stacktrace. I hope that information is sufficient and helpful.
I found the problem. It had nothing to do with the code, persay. It was the manifest file.
Here was the manifest file, as it was originally created by Eclipse:
Manifest-Version: 1.0
Here it is after I modified it:
Manifest-Version: 1.0
Class-Path: blowfishj.jar
I didn't realize that the Java Control Panel is supposed to show the blowfishj.jar file in its Resources. I see my Java Applet, there, but the blowfishj.jar file was not there, until I made the change to this manifest file. I didn't realize it needed the manifest file.
I don't understand why it needs this in the manifest file and why this modification fixes it, though. Maybe someone can answer that question.
Thank you.

Access restriction on JARs (what is the proper way to get VecMath?)

I need Vecmath, so I went to Oracle to get the latest version of Java 3D. I didn't see a place to download a JAR, but it had an installer. I downloaded and ran it. I assume that the installer added the JAR as a JDK extension
I restarted Eclipse, and it sees that VecMath is present. However, it still complains:
Access restriction: The type Vector3f
is not accessible due to restriction
on required library C:\Program
Files\Java\jre6\lib\ext\vecmath.jar
Bummer. I am able to change it from a compiler error to a warning, but it crashes as soon as I try to run it. Although, I'm using this in conjunction with OpenGL, and the crash could have nothing to do with vecmath. I'm not sure. Here is the exception:
Exception in thread "Timer-0" javax.media.opengl.GLException: java.lang.RuntimeException: javax.imageio.IIOException: Can't read input file!
at com.jogamp.opengl.impl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:101)
at com.jogamp.opengl.impl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:192)
at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:591)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:301)
at com.jogamp.opengl.util.AnimatorImpl.display(AnimatorImpl.java:50)
at com.jogamp.opengl.util.Animator.display(Animator.java:154)
at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:95)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Caused by: java.lang.RuntimeException: javax.imageio.IIOException: Can't read input file!
at cs4620.nth23.assignment1.GraphicsApp.loadTextures(GraphicsApp.java:98)
at cs4620.nth23.assignment1.GraphicsApp.init(GraphicsApp.java:65)
at com.jogamp.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:111)
at com.jogamp.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:124)
at javax.media.opengl.awt.GLCanvas$InitAction.run(GLCanvas.java:643)
at com.jogamp.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:273)
at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:674)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at cs4620.nth23.assignment1.GraphicsApp.loadTextures(GraphicsApp.java:95)
... 14 more
I'm using:
x84 Windows 7
Eclipse Helios
JavaSE-1.6
JDK 1.6
What am I doing wrong here?
The exception was caused by being unable to find a file referenced in the code - it has nothing to do with VecMath.
The app runs fine, despite the access restrictions. I don't know if it'll come back to bite me later or what.

Categories