You can access classpath resource in a number of ways. 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. With the file in the project root, you must configure the build to include the file. First thing you would need to do (in this particular) case is make sure that the file get built into the classpath. If so, then you will want to build the file into the classpath, and access it via an URL. 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). 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. So in this case it will fail, as there is not bin\word.txt For instance, running the code from the command line like in the example above, the working directory is the bin. Similarly, if this was your project structure ProjectRoot\src\word.txt, then the path "src/word.txt" would be valid.įor one, the working directory could always change. 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. With your IDE (at least all the ones I've worked with), the working directory is the ProjectRoot. The working directory is C:\EclipseWorkspace\ProjectRoot\bin. The working directory, can be described as this:Ĭ:\EclipseWorkspace\ProjectRoot\bin > java 1 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". Put the word.txt directly as a child of the project root folder and a peer of src Project_Rootĭisclaimer: I'd like to explain why this works for this particular case and why it may not work for others.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |