How can i find files which are located in different packages of the same project?
My Eclipse package is organized as follow:
There are these folders:
src/main/java
input
into the folder src/main/java i have the package main which contains my Main class, while into the input folder i have an .xml file which i want to edit from the Main. My problem is how can i know the relative path of my .xml file in the input folder from my Main class?
You can use ".." in the relative path to go to the folder above for example in your case : ../../../input/your.xml to get to the xml
You are terribly wrong. It doesn't matter where your source file or class file located. Only thing is from where you are running your program from that location, you can calculate the relative path for the particular xml file.
Basically if you are running from eclipse the project root directory should be your project root, not under src/classes. You write below to see your current directory, and based on that you can create/modify your relative path to your xml.
Path path = Paths.get("");
System.out.println("REL PATH" + path.toAbsolutePath().toString());
Related
Lets say a example project structure
/src/java/main/com/example - the path to the base package
/src/java/resources/config.yml
I would want the config.yml to be in the base directory of the jar so it can get detected, but instead it gets compiled into BOOT-INF/classes/, so how would tell maven to put a file into a specific path?
I'm working on a project for school that needs to read from text files inside the project directory. I have it working but only because I have the filepath hardcoded to my computer.
i.e.
String path = "C:\\Users\\MyName\\workspace\\ProjectName\\"
If I sent it to my teacher, the filepath would result in an error.
Is there a way I can set the filepath to wherever the project is stored, from inside the project?
Just put the file name.
String path = "XPTO.txt"
This means your file is in the project root.
Resource files can be place relative to the class files in your project (in this manner, they can be packaged together with class files as a jar file). To access these from within your project, you can use Class.getResource() or Class.getResourceAsStream. For instance...
InputStream is = MyClass.class.getResourceAsStream('path/to/file');
Where 'path/to/file' is the path relative to where MyClass resides. Note the lack of a '/' at the beginning of this path - if it began with '/' it would be an absolute path relative to the highest package level of the project. Also note that one can use a relative file path to read a file external to the class package directory structure.
Just do WhateverNeedsAPath("Something") //path will be whereever/ProjectName/Something
This might help you also: How to define a relative path in java
I have a class in a package com.mwerner.utils that needs the path for an getResourceAsStream() call
the file it is supposed to load is located in a subfolder of the project root i.e.:
/src/com/mwerner/utils/myfile.java has to load
/res/file.xml
I tried stuff like
/res/file.xml
../res/file.xml
res/file.xml
What is the right one?
EDIT:
I am using Xstream to parse the XML into objects. The line of code in question is:
ObjectInputStream in = xstream.createObjectInputStream(Utils.class.getResourceAsStream("res/file.xml"));
I get an IOException with unknown source
Turns out Eclipse puts the entire content of the res folder in the root folder of the bin folder. Therefore the path is just very simply /file.xml. I tried putting it into a subfolder of res called xmls and the path then is /xmls/file.xml
I also went to the source tab of the java build path and added the res folder.
I was wondering how to specifiy a relative path name to a text file that is stored in my src folder of a java project. I would like to store a string with the path of this text file. For example if I had example.txt located in the src folder of my java project how would I go about finding its relative path? I am also doing this in my main so I'm having trouble using .getResource(). How would I do this? Thanks.
My files path is as followed from the properties in eclipse
/MyProject/src/data.txt
I've tried:
String path = "/MyProject/src/data.txt";
But that doesn't work?
If you are using eclipse, place your text file in the root of the project folder, outside the /src and /bin folders. It should be now accessible via a relative path directly.
If you want to access a file in src folder, you have to append the /src/ prefix before the file path/name
Use the src/ prefix before the file path/name.
String path = "src/data.txt";
The path is relative to the directory you execute the "java" command.e.g.
/opt/projects/myproject>$ java -cp <whatever your classpath is that contains your class files> com.mycompany.mypackage.MyJavaClass
in this case the MyJavaClass would find files relative to the directory
/opt/projects/myproject
There is another way to do this if you like: you can use load resources, which are found via the classpath mechanism.
getClass().getResource("foo.txt");
You can see this posting for more info
Preferred way of loading resources in Java
I have a problem where I can't seem to link to a xml file, see the layout below:
Folder Name
-Folder
-Folder
-SourceFiles
-packagename
-all my java files
-myXml.xml
Build is where all the class files etc is stored.
src is where the projectFolder is, and within it the java files
Code I am using to link XML File for Synth: SynthDialog.class.getResourceAsStream("synthtest/synthDemo.xml")
Now I want to link to the myXML.xml file in the top-level folder. It would be the PHP Equivelent of ../../Folder/
Thanks
You appear to be attempting to access the file using getResourceAsStream with a relative name. If that is the case, then the resource should be in located in a JAR file or directory on the classpath, and the location will be resolved relative to the FQN of the class.
I can't tell where the ".class" files are located in the tree, or how your classpath is set up, so I can't be more specific.
UPDATED
If you are executing out of that build directory, then your build process needs to copy the XML file to the appropriate place in the build tree so that the class-relative path ends up referring to the file. (Or use a path that starts with "/" so that you don't depend on the classes FQN at all.)
In the long term, you will probably execute out of a JAR file, and the data file will need to be inside it.
Use "getSystemResourceAsStream" instead of "getResourceAsStream" to access files outside of your codebase.