So I have a .jar file in a folder and I have some input files in that folder. However, the program looks for the file in the home folder (several layers up). I want it obviously to read it from the folder that it's in but I don't want to be explicit about the file path to my folder because other people won't necessarily put their .jar file in the same spot.
Is there a way to read a file directly outside of the jar file? If not, is there a way to do this without hard-coding the file path?
edit:
here's the code. It just checks if the input files exist.
package main;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Created by chris on 12/1/15.
* Control class that will be run when the jar is run.
*/
public class run {
public static void main(String[] args) throws Exception {
if (!(new File("settings.txt").exists())) {
start.run();
}
if (!(new File("api_key.txt").exists())) {
alert.display("Make your api_key.txt please.");
} else {
gatherData.run();
}
}
}
edit 2:
I've tried adding relative references with "./" at the beginning but that doesn't work either.
If you can rely on your .jar file being on the file system, you can get the absolute path of it by
new File(run.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
which can result in a SecurityException if a SecurityManager is present and not allowing this.
Another possibility would be to use
(new File(System.getProperty("java.class.path"))).getAbsolutePath();
The folder of the jar file can then be obtained using getParentFile()
If I am not wrong you are trying to access a file right in the same folder as that of the .jar file.
This can easily be done using the relative URL. By relative URL, I meant using
new File("./settings.txt"), this searches for the file in the folder same as that of the running .jar file. however you can use "../settings.txt" to look for the file one folder up.
"./" refers same directory "../" refers one directory up.
Just use ./ before file names.
The JAR files not neccesarly load from file system, and when loaded from file system can be any directory where the application start from - so relative path is not a good idea. The JAR files can from other types of source not only FileSystem, because it can be stream. I think the best way if there is a system parameter where you can pass the directory or working directory when you start the JAR file.
Related
As you can see, the file is right inside the same folder where the class file is. While I tried to have it print the path to it, that is a bummer because no matter what name I type it will always type a full path to that invented file name. The file is comma separated, that I copied pasted into gedit and tried all kinds of extensions, csv, txt and no extension.
What I am trying to do is import it into an array, but the error is clear, no such file or directory.
You can try getting the path of the root folder from System.getProperty(). Then just append underlying folder names till the required filename.
private static String CLASS_PATH = System.getProperty("user.dir");
public static String FILE_PATH= CLASS_PATH + File.separator + "Source Packages"+ File.separator + "javaapplication2"+ File.separator + "pedidos.txt";
You should place files that are needed by your program in the classpath of your application so that they will get packaged in your JAR file. You can then read them with getResourceAsStream() Here's an idea of how that would work.
public class ArrayMain {
public static void main(String[] args) {
Scanner in = new Scanner(ArrayMain.class.getResourceAsStream("numbers.txt"));
String[] numbers = in.nextLine().split(",");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
The file numbers.txt is a simple file that looks like this:
1,2,3,4,5,6,7,8,9
Do not use relative paths to get files packaged with your application, it's dirty and senseless.
Path are relative to the folder in which was launched the java process. It's (usually) not your application source directory and never you file.java directory.
Even then, it's a terrible practice to put files (resources) inside your application source code. Because your java files needs to be compilated (and often packaged into a jar), the current architecture is likely to have no more meaning once packaged.
If you want to refer to a file on your computer, put it in a sensible folder (like /user/documents) and get it with its absolute path.
If you want the file to be packaged with your application, put it in a resource folder, package it in a jar and get the path with a classpath:// scheme.
The file needs to be placed at the root folder of your project.
I have a file named "word.txt".
It is in the same directory as my java file.
But when I try to access it in the following code this file not found error occurs:
Exception in thread "main" java.io.FileNotFoundException: word.txt
(The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Hangman1.main(Hangman1.java:6)
Here's my code:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
Put the word.txt directly as a child of the project root folder and a peer of src
Project_Root
src
word.txt
Disclaimer: I'd like to explain why this works for this particular case and why it may not work for others.
Why it works:
When you use File or any of the other FileXxx variants, you are looking for a file on the file system relative to the "working directory". The working directory, can be described as this:
When you run from the command line
C:\EclipseWorkspace\ProjectRoot\bin > java com.mypackage.Hangman1
the working directory is C:\EclipseWorkspace\ProjectRoot\bin. With your IDE (at least all the ones I've worked with), the working directory is the ProjectRoot. So when the file is in the ProjectRoot, then using just the file name as the relative path is valid, because it is at the root of the working directory.
Similarly, if this was your project structure ProjectRoot\src\word.txt, then the path "src/word.txt" would be valid.
Why it May not Work
For one, the working directory could always change. For instance, running the code from the command line like in the example above, the working directory is the bin. So in this case it will fail, as there is not bin\word.txt
Secondly, if you were to export this project into a jar, and the file was configured to be included in the jar, it would also fail, as the path will no longer be valid either.
That being said, you need to determine if the file is to be an embedded-resource (or just "resource" - terms which sometimes I'll use interchangeably). If so, then you will want to build the file into the classpath, and access it via an URL. First thing you would need to do (in this particular) case is make sure that the file get built into the classpath. With the file in the project root, you must configure the build to include the file. But if you put the file in the src or in some directory below, then the default build should put it into the class path.
You can access classpath resource in a number of ways. You can make use of the Class class, which has getResourceXxx method, from which you use to obtain classpath resources.
For example, if you changed your project structure to ProjectRoot\src\resources\word.txt, you could use this:
InputStream is = Hangman1.class.getResourceAsStream("/resources/word.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
getResourceAsStream returns an InputStream, but obtains an URL under the hood. Alternatively, you could get an URL if that's what you need. getResource() will return an URL
For Maven users, where the directory structure is like src/main/resources, the contents of the resources folder is put at the root of the classpath. So if you have a file in there, then you would only use getResourceAsStream("/thefile.txt")
Relative paths can be used, but they can be tricky. The best solution is to know where your files are being saved, that is, print the folder:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
File myFile = new File("word.txt");
System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());
Scanner input = new Scanner(myFile);
String in = "";
in = input.nextLine();
}
}
This code should print the folder where it is looking for. Place the file there and you'll be good to go.
Your file should directly be under the project folder, and not inside any other sub-folder.
If the folder of your project is named for e.g. AProject, it should be in the same place as your src folder.
Aproject
src
word.txt
Try to create a file using the code, so you will get to know the path of the file where the system create
File test=new File("check.txt");
if (test.createNewFile()) {
System.out.println("File created: " + test.getName());
}
I was reading path from a properties file and didn't mention there was a space in the end.
Make sure you don't have one.
Make sure when you create a txt file you don't type in the name "name.txt", just type in "name". If you type "name.txt" Eclipse will see it as "name.txt.txt". This solved it for me. Also save the file in the src folder, not the folder were the .java resides, one folder up.
I have the same problem, but you know why? because I didn't put .txt in the end of my File and so it was File not a textFile, you shoud do just two things:
Put your Text File in the Root Directory (e.x if you have a project called HelloWorld, just right-click on the HelloWorld file in the package Directory and create File
Save as that File with any name that you want but with a .txt in the end of that
I guess your problem is solved, but I write it to other peoples know that.
Thanks.
i think it always boils to the classpath. having said that if you run from the same folder where your .class is then change Scanner input = new Scanner(new File("word.txt")); to Scanner input = new Scanner(new File("./word.txt")); that should work
I have a jav aproject which is build through ant. It write the class files to output/classes/com/... path. One of my java classes needs input stream read from a file that is in a folder one level above output folder. Looks like if copy the file to the package folder under outptu/classes, it seems to work. But I do not want to palce my config file in output folder as it will be cleaned when I do ant clean. I want it to find it look above the output folder, in config folder and load it.
public static final String CONFIG_FILE="/../../../../../../../Config.txt";
public static ConfigObj getConfigObj() throws IOException {
InputStream i=ConfigLoader.class.getResourceAsStream(CONFIG_FILE);
...
I want to know when I want to give raltivepath, what should it be relative to. I tried looking up , it says relative to classloader. What is classloader in this case? Is it output/classes/com....../config folder where my ConfigLoader.class lives?
The problem is that getResourceAsStream() will only load resources from the classpath. I guess you only have output/classes on your classpath, so you will never be able to load the config file via getResourceAsStream() if it's outside that directory. Use a File with an absolut path pointing to the file, or place it in your classpath.
I put some .txt files under the src folder (in the resources folder).
But I can't create a valid File at runtime from this resource.
String path = this.getClass().getResource("/resources/file.txt").getFile();
File file = new File(path);
if (!file.exists()) {
}
I run my program from eclipse. I didn't put in classpath anything.
I want my text files to be embedded into the .jar file, when I run my app I want to grab those files and copy them into some location.
UPDATE
if I do InputStream is = getClass().getResourceAsStream("/resources/file.txt");
I get the stream!!
As you already discovered yourself soon after posting your question, this works:
InputStream is = getClass().getResourceAsStream("/resources/file.txt");
The reason this works, while your original code doesn't, is because a "file" inside in a zip file (a jar file is a zip file) is not a real file until it has been extracted. But extracting the file is what you are trying to do, so at that point in your program, it's not a real file. So this question is an X-Y problem: you wanted to create a File object, but that wasn't possible - you needed to refer back to what you were originally trying to do, which was read from the zip entry.
You said that you are using eclipse, and that you dragged and dropped your text files into the "src" package. "src" is not a package. It is simply a file system directory. By default in a Java project in eclipse all your source code is stored in a directory called "src" and all your .class files are stored in a directory called "bin". getClass().getResource() resolves to the location of your .class files. You must move the text files into the "bin" directory.
What package is your class in?
I wrote very similar code to yours in the default package and ran it in eclipse.
import java.io.File;
public class ResourceTest {
public static void main(String[] args) {
ResourceTest rt = new ResourceTest();
rt.openFile();
}
public void openFile() {
String path = this.getClass().getResource("/resources/file.txt").getFile();
File file = new File(path);
System.out.println(path);
System.out.println(file.getAbsolutePath());
System.out.println(file.exists());
}
}
I see this output:
/C:/Users/rab29/Documents/eclipse/Overflow/bin/resources/file.txt
C:\Users\rab29\Documents\eclipse\Overflow\bin\resources\file.txt
true
Currently, in my eclipse project, I have a file that I write to. However, I have exported my project to a JAR file and writing to that directory no longer works. I know I need to treat this file as a classpath resource, but how do I do this with a BufferedWriter?
You shouldn't have to treat it as a classpath resource to write to a file. You would only have to do that if the file was in your JAR file, but you don't want to write to a file contained within your JAR file do you?
You should still be able to create and write to a file but it will probably be relative to the working directory - the directory you execute your JAR file from (unless you use an absolute path). In eclipse, configure the working directory from within the run configuration dialog.
You're probably working in Linux. Because, in Linux, when you start your application from a JAR, the working directory is set to your home folder (/home/yourname/). When you start it from Eclipse, the working directory is set to the project folder.
To make sure you really know the files you are using are located in the project folder, or the folder where your JAR is in, you can use this piece of code to know where the JAR is located, then use the File(File parent, String name) constructor to create your files:
// Find out where the JAR is:
String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
path = path.substring(0, path.lastIndexOf('/')+1);
// Create the project-folder-file:
File root = new File(path);
And, from now on, you can create all your File's like this:
File myFile = new File(root, "config.xml");
Of course, root has to be in your scope.
Such resources (when altered) are best stored in a sub-directory of user.home. It is a reproducible path that the user should have write access to. You might use the package name of the main class as a basis for the sub-directory. E.G.
our.com.Main -> ${user.home}/our/com/