This question already has answers here:
Read properties file outside JAR file
(8 answers)
How to get the path of a running JAR file?
(33 answers)
Closed 6 years ago.
I have a folder structured like this:
MyFolder:
file1.xml
file2.xml
project.jar
But if in a class I use:
File f = new File("file1.xml");
I receive an error, because it doesnt find the file. Why?
You should use a relative path in your code.
Example: File f = new File("./file1.xml");
If you are using Windows the code you posted will work, but not on Linux where the default parent file is your home.
But you can do in any OS by using:
public class MyClass {
public void loadFile() {
URL url = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
File jar = new File(url.toURI());
File f = new File(jar.getParent(), "file1.xml");
//your code
}
}
PS: This needs to be inside project.jar because you are getting the location where you jar file is.
Related
This question already has answers here:
How to read a file from jar in Java?
(6 answers)
How to read a text file inside a JAR? [duplicate]
(4 answers)
Closed 3 years ago.
I have class files and a text file wrapped up in a jar. This problem has been solved on the internet before, but when I try it I get a null pointer exception, or File f.exists() returns false. It should be noted that my code is not in a package. It should be noted that when help.txt is dropped in the same folder as the jar, then it works.
`MyClass z = new MyClass();
String helpPath = z.getClass().getClassLoader().getResource("help.txt").toString();
File f = new File(helpPath);
if (f.exists()){
Desktop d = Desktop.getDesktop();
d.open(f);`
It should also be noted that I have code written to open powershell and then java my class file, with no specified classpath.
` Runtime.getRuntime().exec(new String[]{"cmd","/c","start","powershell","-noexit","/c","java -jar \"" + filename + "\""});`
This question already has answers here:
How to read and write excel file
(22 answers)
Closed 3 years ago.
What should be the syntax when the excel file that I'm trying to call is imported within the project.
The syntax you are using is correct but make sure your using the correct path of your local file.
When your local file is in the same package/folder where you write your codes.Just write down its file name.
var file = new FileInputStream("Dashboard.xlsx");
or try using Absolute path of your filename.
var file = new FileInputStream("M3A FrontEndPoc/Dashboard.xlsx");
Use :
public class ReadFileExcel
{
public static void main(String[] args) throws IOException
{
ClassLoader classLoader = new ReadFileExcel.getClass().getClassLoader();
File file = new File(classLoader.getResource("./dashboards.xlsx").getFile());
// replace the below one with the above but just change the name of the class
var file = new FileInputStream("Dashboard.xlsx");
}
}
Note : Build the project, otherwise classloader will not able to read.
This question already has answers here:
How to get the real path of Java application at runtime?
(15 answers)
Closed 6 years ago.
I'm trying to load a .txt file into an arrayList in java using a combination of relative paths.
My jar file is in /usr/tool/dist/tool.jar
The file I want to load is in /usr/tool/files/file.txt
I think I was able to retrieve the path of my tool.jar, but how can I go from that path to the one where my file is?
I have the following code
// String path should give me '/usr/tool'
File f = new File(System.getProperty("java.class.path"));
File dir = f.getAbsoluteFile().getParentFile();
String path = dir.toString();
String table1 = this should represent /usr/tool/files/file.txt
BufferedReader buf_table1 = new BufferedReader(new FileReader(new File(table1)));
To find the path of your jar file being executed, java.class.path is not the right property. This property may contain more than one file, and you cannot know which is the right one. (See the docs.)
To find the path of the correct jar file, you can use this instead:
URL url = MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Where MainClass the main class of your tool, or any other class in the same jar file.
Next, the parent File of a file is its directory. So the parent File of /usr/tool/dist/tool.jar is /usr/tool/dist/. So if you want to get to /usr/tool/files/file.txt, you need to get the parent of the parent, and then from there files/file.txt.
Putting it together:
File jarFile = new File(MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
File file = new File(jarFile.getParentFile().getParent(), "files/file.txt");
This question already has answers here:
FileNotFoundException, the file exists Java [closed]
(2 answers)
Closed 7 years ago.
Hello I have this in my code
File file = new File("words.txt");
Scanner scanFile = new Scanner(new FileReader(file));
ArrayList<String> words = new ArrayList<String>();
String theWord;
while (scanFile.hasNext()){
theWord = scanFile.next();
words.add(theWord);
}
But for some reason I am getting a
java.io.FileNotFoundException
I have the words.txt file in the same folder as all of my .java files
What am I doing wrong? Thanks!
Tip: add this line to your code...
System.out.println(file.getAbsolutePath());
Then compare that path with where your file actually is. The problem should be immediately obvious.
The file should reside in the directory from which you execute the application, i.e. the working directory.
Generally it's a good idea to package data files with your code, but then using java.io.File to read them is a problem, as it's hard to find them. The solution is to use the getResource() methods in java.lang.ClassLoader to open a stream to a file. That way the ClassLoader looks for your files in the location where your code is stored, wherever that may be.
try:
URL url = this.getClass().getResource( "words.txt" );
File file = new File(url.getPath());
You haven't specified an absolute path. The path would therefore be treated as a path, relative to the current working directory of the process. Usually this is the directory from where you've launched the Main-Class.
If you're unsure about the location of the working directory, you can print it out using the following snippet:
System.out.println(System.getProperty("user.dir"));
Fixing the problem will require adding the necessary directories in the original path, to locate the file.
This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 3 years ago.
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile".
However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
Looks file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.