I'm using this code File serviceAccountFile = new File(Main.class.getResource("/serviceAccountKey.json").getFile()); to access a json file inside my "resources" folder. It works fine when I'm running it from intellij. But i get this error when running the program from the jar.
java.io.FileNotFoundException: file:\C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\serviceAccountKey.json (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at ashirwada.pos.Firebase.initializeFirebase(Firebase.java:27)
at ashirwada.pos.Main.init(Main.java:33)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
I have other files like fxml files and jpg inside my resource file. They get detected and runs fine. This json file is the only thing thats giving me problems. I opened the Jar with winrar and the json file is there with the rest of the fxml files and the jpg. I'm using maven to compile my jar with the dependencies i need.
That's because after you package your resource into a jar, the string you get by URL.getFile() would be invalid for the constructor File(String path).Use getResourceAsStream instead may solve your problem.
System cannot resolve file C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\UI\serviceAccountKey.json due to the file is in jar file
You should extract the jar in to a folder and read json file from there
Or you can read file as a resource
InputStream in = etClass().getResourceAsStream("/UI/serviceAccountKey.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try this
File(Main.class.getClassLoader().getResource("/serviceAccountKey.json").getFile());
Related
while starting jboss I am getting below error:
java.io.FileNotFoundException: file:\C:\workspace4.3.2\workspacePLE.metadata.plugins\org.jboss.ide.eclipse.as.core\JBoss_AS_4.21507285539456\deploy\LocalDeploy.ear\ple.war\WEB-INF\lib\ple-data.jar!\data\config\HTMLEntities.res (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at com.inetpsa.ple.utilitaires.PleCharEntities.initialize(PleCharEntities.java:37)
at com.inetpsa.ple.initialisation.OUInit.initHtmlEntities(OUInit.java:340)
at com.inetpsa.ple.initialisation.OUInit.interneInit(OUInit.java:367)
at com.inetpsa.ple.initialisation.OUInit.reinit(OUInit.java:424)
at com.inetpsa.ple.initialisation.OUInit.init(OUInit.java:412)
But the file is present at this location (however it is inside a jar).
The code for getting file path is:
String htmlEntFileName = OUInit.class.getClassLoader().getResource("data/config/HTMLEntities.res").getPath();
Please let me know if I am missing anything.
You are using FileInputStreamso I assume you ase the path to create new FileInputStream, but once the file is inside a jar it can't be accessed as a file anymore.
The file can be accessed as an InputStream using either Class.getResourceAsStream() or Class.getResource().openStream().
I'm trying to create a CSV file in a Java program. No matter where the file is located, I've this error:
Exception in thread "main" java.io.FileNotFoundException: <location> (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
I have the same error for the .PDF and .DOC files.
I have no error with .TXT, .JSON ......
I have the administator rights.
I think it's Microsoft Office which protect .DOC and .CSV files.
When you call new File your are supposed to give the file name, not only the directory you want to put your file in.
Try with something like
File file = new File("D:/Data/" + item.getFileName());
Try printing the fileObj.getAbsolutePath(); to see if the file object is referring to the correct location/file.
If it is correct, then the problem is with access.
Remember, you may see the Access Denied error even if the file already exists and is being used by another program.
You should consider posting some code so that we can review the code blocks to identify a solution.
Currently I'm trying to make a level editor for a Java game that I created.
My problem is that my program doesn't save the edited levels.
Here is the code that I made so far:
Path path = Paths.get(gameFile.getAbsolutePath());
FileSystem fs = FileSystems.newFileSystem(path, null);
Path p = fs.getPath("rpg"+fs.getSeparator()+"levels"+fs.getSeparator()+"level"+(short)level+".png");
OutputStream os = Files.newOutputStream(p);
ImageIO.write(img, "png", os);
os.close();
fs.close();
gameFile is the .jar file of my game which contains the levels and
the levels are saved as .png files.
I have tried to save my picture outside of the jar and it worked so i guess i made a mistake with the FileSystem or the Path.
The Path in the jar is rpg/levels/level*.png
Edit:
I changed
OutputStream os = Files.newOutputStream(p);
to
OutputStream os = Files.newOutputStream(p,StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING);
but now I get an error when closing the fileSystem
java.nio.file.FileSystemException: G:\RPG.jar: The process cannot access the file because it is being used by another process.
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.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source)
at java.nio.file.Files.delete(Unknown Source)
at com.sun.nio.zipfs.ZipFileSystem.sync(ZipFileSystem.java:1294)
at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:277)
at de.jorolf.editor.LevelEditor$5.actionPerformed(LevelEditor.java:213)
You can not save anything inside of a running jar-File (related: updating-a-jar-whilst-running).
Also, if you want to save something inside of a not running jar-File, then you also need to save the file compressed. For this task you can simply add a ZipOutputStream to your stream-queue (Java-Doc: ZipOutputStream.html). Also java.util.jar.JarFile and java.util.jar.JarEntry may be helpful.
For your general task, you should save the level file outside of the jar. You may distribute the game in a folder game with /game/theGame.jar and /game/firstLevel.lvl. Then the level is outside of the jar and you can modify it.
Also you may use common directories as other games do, like Windows Documents or AppData for game specific settings and data.
MyDocuments in Java: FileSystemView.getFileSystemView().getDefaultDirectory().getPath()
AppData in Java: System.getenv("APPDATA")
In my plugin project, I added a new file called "database.properties", when I try to run the plugin I am getting below exception:
java.io.FileNotFoundException: database.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
Here is my project structure:
Also I have included that file in build properties, screenshot is shown here:
I tried cleaning, restarting eclipse, building etc. But still system is unable to find the file. Can anyone help me? what I am missing here?
You have to access the file like that:
Bundle bundle = Platform.getBundle("your_plugn_id");
InputStream is = bundle.getEntry("database.properties").openStream();
And includeit in your build.properties:
bin.includes = META-INF/,\
OSGI-INF/,\
database.properties/,\
.,\
plugin.xml
I want to set the path for my file that i'm going to read without having to specify the root directories or the whole path.
like this : File file = new File("Text01.txt");
or Path file = Paths.get("Accounts.txt");
instead of : Path file = Paths.get("C:\\Users\\B_HITMAN\\Documents\\NetBeansProjects\\BankAccountGUI\\src\\bankaccountgui\\Accounts.txt");
I even tried to put the file inside the build directory of netbeans, but it didn't work either. it show's this message :
Exception in thread "main" java.io.FileNotFoundException: Text01.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at assignment01.Assignment01.main(Assignment01.java:19)
Java Result: 1
Place your file in the BankAccountGUI directory(project's root directory). And then try to access the file like this:-
File file = new File("Text01.txt");
Note:- Your file should be at the same level as the src folder.