Unable to create a file path in java - java

I'm trying to save my file into a perticular directory. but i get the below Exception
Exception in thread "main" javax.xml.transform.TransformerException: java.io.FileNotFoundException: D:\News\nxis\NewFiles\I0cbf74105a2d11e5b730aca98fc673fd.nxi (The system cannot find the path specified)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at org.NXI.CreateNxi.createFiles(CreateNxi.java:49)
at org.NXI.GetInput.main(GetInput.java:24)
Caused by: java.io.FileNotFoundException: D:\News\nxis\NewFiles\I0cbf74105a2d11e5b730aca98fc673fd.nxi (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
... 4 more
---------
java.io.FileNotFoundException: D:\News\nxis\NewFiles\I0cbf74105a2d11e5b730aca98fc673fd.nxi (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at org.NXI.CreateNxi.createFiles(CreateNxi.java:49)
at org.NXI.GetInput.main(GetInput.java:24)
Here the path that is available in real is
String output = "D:\\News\\nxis\\" + replaceGuid + ".nxi";
But i'm trying to add a new folder inside it. I'm using the below command
String output = "D:\\News\\nxis\\New Folder" + replaceGuid + ".nxi";
When i run my program with the second step. It throws me an error. please let me know how can i fix it.
Thanks

this will fix your problem:
new File(output).mkdir();
mkdir() creates the directory named by this abstract pathname.

Make sure that D:\News\nxis\ directory path is existing if not you have to create it.

Related

java.io.FileNotFoundException on Properties FileInputStream in CMD

I'm loading a Property file with the following method:
private final String utitt=Paths.get(".").toAbsolutePath().normalize().toString();//
...
properties.load(new FileInputStream(utitt+"/beallitasok/lang.set"));
...
This works fine within NetBeans without problems, but the jar file fails in command prompt:
java.io.FileNotFoundException: C:\java\IKE\dist\beallitasok\lang.set
(A rendszer nem találja a megadott fájlt)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at IKE.IKE.fordit(IKE.java:1519)
at IKE.IKE.access$300(IKE.java:81)
at IKE.IKE$IngatlanokAblak.(IKE.java:9411)
at IKE.IKE.(IKE.java:544)
at IKE.IKE.main(IKE.java:697)
I've copied the "beallitasok" directory there (to c:\java\IKE\dist in my case), so the file is actually there. I'm using some files to translate certain strings and save/read some settings. I've checked whether i'm just blind, so just opened the file with a "more C:\java\IKE\dist\beallitasok\lang.set" command and it works.
Where or how shall i search for a clue? I'm not a pro, so thanks for any help.
PS: (A rendszer nem találja a megadott fájlt) = The System can't find the specified file. Translated from the Hungarian language.

FileNotFoundException: path syntax wrong

I am trying to export my project as an executable jar file in eclipse.
My jar file looks like this:
\com
\de
\org
\res
I've got some xml files in the res folder and now I have some problems getting these files with the FileReader
java.io.FileNotFoundException: jar:file:\C:\Users\Patrick\Desktop\Brainwav3.jar!\res\de.xml (Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at de.brainwav3.language.LanguageHandler.load(LanguageHandler.java:98)
at de.brainwav3.core.Launch.main(Launch.java:24)
The path gets generated like this:
URL url = getClass().getResource("/res/de.xml");
FileReader reader = new FileReader(url.toString());
So I don't really have control over the path.
I resolved this problem with copying the required files next to the jar file and changing the url to another path but I want all ressources to be inside the jar file

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.

FileNotFoundException while trying to write an image using thread in 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\

Java get resource InvalidJarIndexException

I have a class names test.java and another file "log4j.properties" in the same folder but following call
getClass().getResource("log4j.properties")
fails with
ms
Trace:
sun.misc.InvalidJarIndexException: Invalid index
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.findResource(Unknown Source)
at sun.misc.URLClassPath.findResource(Unknown Source)
at java.net.URLClassLoader$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.Class.getResource(Unknown Source)
But getClass().getClassLoader().getResource("log4j.properties") works. What can be the reason?
The InvalidJarIndexException is normally caused by a corrupt INDEX.LIST file in the JAR. So check the JAR for any problems. See http://littletechsecrets.wordpress.com/2008/12/01/why-does-invalidjarindexexception-occur/
The different results you describe are probably related to Class.getResource() resolving a path relative to the classes package, and ClassLoader.getResource() resolving a path relative to the root. So getClass().getResource("/log4j.properties") might fix your problem.

Categories