Can't find resources runnable jar - java

I can't load an resource file after extracting a project as a runnable jar.
It works in Eclipse but after exporting it throws a FileNotFoundException.
I have tried to put the res folder next to the .jar file but nothing helps. I've tried with JarSplice and got it running with all the libraries but it stops with the resource file. The object file is located in a source folder.
What can I do?
Code
FileReader fr = null;
try {
fr = new FileReader(new File("res/" + fileName + ".obj"));
} catch (FileNotFoundException e) {
System.err.println("Couldn't load object file!");
e.printStackTrace();
}
EDIT: By opening the runnable .jar file in 7zip I can see that the whole /res folder has disappeared during the exporting and the files in the directory now lies directly in the root folder of the .jar file.

Based on your code, the res folder should be placed directly off the present working directory, which should be the directory where you are running the Java command from. For an example, see this question on how to determine the current working directory from inside Java.

Use Path instead of new File(string).
FileReader fr = null;
try {
fr = new FileReader(Paths.get("res/" + fileName + ".obj").toFile());
} catch (FileNotFoundException e) {
System.err.println("Couldn't load object file!");
e.printStackTrace();
}

Related

Netbeans- reach resources folder

I know that this question it is similar to this one but it is different. I am trying to open a pdf file that it is in the resources folder of netbeans.
Right now I am in the EventoService.java and I have created a file object to open the pdf file (justificante.pdf) in "Other Resources" folder. I have tried to reach the pdf file like in the link before but it doesn't work because of the constructor. How can I reach it? Thank you in advance.
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File(getClass().getClassLoader().getResource("resources/justificante.pdf"));
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}

Java properties file is saved in system32 folder

So, my I have a method that saves some data in a properties file but something weird happens. See, lets say I have the JAR file on desktop. If I open it directly from there (double click, etc) the properties file is saved in the desktop, as should be. However, if you drag the JAR to the Windows start list and open it from there, the properties file will be saved in the System32 folder.
Here is the method:
private void saveAncientsData() {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("ancients.data");
File file = new File("ancients.data");
// set the properties value
for (int x = 0; x < currentLvlSpinnerFields.size(); x++) {
prop.setProperty(ancientNames[x], currentLvlSpinnerFields.get(ancientNames[x]).getValue().toString());
}
// save properties to project root folder
prop.store(output, null);
JOptionPane.showMessageDialog(this, "Data successfully saved in \n\n" + file.getCanonicalPath(), "Saved", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Would appreciate any help, since I am clueless.
Thanks in advance!
according to your code you haven't give path of property file to create in desktop.
output = new FileOutputStream("ancients.data");
so your property file will be created in same directory where your jar file exists .
but if you run this .jar file from a parent process your jar file created in the directory where that parent process exists.
i guess when windows starts a specific process exist in win32 directory execute start-up programs .i think it's userinit.exe . so your prop file will be created in System32 directory .
if you want property file to create in desktop you can put your jar file in desktop and add a shortcut to .jar or you can give full-path to your desktop like
output = new FileOutputStream(System.getProperty("user.home") + "/Desktop/"+"ancients.data");
edit
to understand this problem
1) create a folder named example in desktop.and then create 2 folders path1 and path2 .then add .jar to path1 folder
2) double click jar in path1 .and a property file will be created in path1 as you expected .
3) delete property file.open command prompt in path2 . To run Prop.jar file in path1 . type call "pathtodesktop/example/path1/Prop.jar" hit enter.
.property file will be created in path2 instead of path1 that's what happening in your case.

What is the root place of FileReader searching for files?

I've tried to load file to use by scanner like this:
try {
Scanner scanner = new Scanner(new FileReader("map.txt"));
}catch (FileNotFoundException e) {
e.printStackTrace();
}
But I get the FileNotFoundException error even thought I place map.txt file in class folder. Also I created special folder and marked it as a resources root in IntelliJ IDEA but still it doesn't work. How can I know where FileReader is searching for file then?
This might help you. Use File#getAbsolutePath() to check for the path.
File reader = new File("abc.txt");
System.out.println(reader.getAbsolutePath());
Some more ways to read from project
// Read from resources folder parallel to src in your project
File file1 = new File("resources/abc.txt");
System.out.println(file1.getAbsolutePath());
// Read from src/resources folder
File file2 = new File(getClass().getResource("/resources/abc.txt").toURI());
System.out.println(file2.getAbsolutePath());

Specify a Relative Path from a Package for a new File

I'm trying to create a File object in order to save my properties file. I need to know how to specify a relative path from my package though because the below code does not work.
try {
File file = new File(new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()), "com/configuration/settings.properties");
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
properties.store(fileOutputStream, null);
}
} catch (IOException | URISyntaxException ioe) {
System.out.println(ioe);
}
Just replace this line:
File file = new File("/com/configuration/settings.properties");
with:
File file = new File(new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()), "com/configuration/settings.properties");
Relative path depends on current working directory from where you are running your program.
If you are running in IDE, IDE may set working directory path to Project directory. And How your program run depends on classpath to jar/claases in your program.

Create Directory along with file using file stream in java

I have a String like this "D:/Data/files/store/file.txt" now I want to check ,is directory is already exist or not, if not I want to create directory along with text file. I have tried mkdirs() but its creating directory like this data->files->store->file.txt. means its creates file.txt as folder, not a file. can any one kindly help me to do this. thanks in advance.
You need to run mkdirs() on parent directory, not the file itself
File file = new File("D:/Data/files/store/file.txt");
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Here you go...
boolean b = (new File("D:/Data/files/store/file.txt").getParentFile()).mkdirs();

Categories