I am trying to return my project's file path to then create a file in said path. I successfully created the file in the path but I used the static path to my project rather than resolving it programatically like I need to do.
Using the docs i've tried creating my file:
Path path = Paths.get("C:\\folder\\folder\\folder\\folder\\folder\\report\\");
String filePath = path.toString() + "fileName.pdf";
createFile(filePath, data, moreData);
Question:
What if someone else uses a D: drive or other? How do I resolve the report folder if that is the case?
Use a relative path to the file, rather than an absolute path.
Path path = Paths.get("reports/fileName.pdf");
String filePath = path.toString() + "fileName.pdf";
createFile(filePath, data, moreData);
Related
I want to specify the path dynamically. myapp/CopyFolder and myapp/RunFolder's are inside application like myapp/WEB-INF. The code I have given below is in .java file(in eclipse) and in .class file(in tomcat inside myapp/WEB-INF/classname/packagename/). My deployment is in tomcat.
try {
functionNamesObject.Integration(
".txt",
path+"\\CopyFolder",
path+"\\RunFolder",
"app.exe",
"Input.txt"
);
I want path to be dynamic when I call above function. I tried with getResource("MyClass.class") ,new File("").getAbsolutePath(); and System.getProperty("user.dir") but no use. Is there any other way?
You can get the path value as below:
URL resource = getClass().getResource("/");
String path = resource.getPath();
This will return the absolute path to to your myApp/WEB-INF/classes directory.
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'm trying to load an image file.
File imageCheck = new File("Users/me/Documents/workspace/ChineseChess/src/RCar.gif");
if (imageCheck.exists())
System.out.println("Image file found!");
else
System.out.println("Image file not found! :(");
button.setIcon(new ImageIcon("Users/me/Documents/workspace/ChineseChess/src/RCar.gif"));
Should I put RCar.gif in the src or bin folder of an eclipse project?
How would I refer to it without the explicit path? (I'm using a mac. I believe it should be something like ~/RCar.gif but am having trouble finding the correct syntax online.
Thanks!
If you want to use relative paths, they are going to be relative to the project's root, or when its deployed, the directory that the jar file resides in. You can use src/RCar.gif as your filename.
It would be better if you created a separate folder for resources, then address them with new File("res/RCar.gif"); Another option is to put them in the src folder and address them using the classloader.
If I recall correctly, Eclipse requires all resources to be placed in the resources directory. These will be automatically bundled with your application when you export it.
At runtime, you would simply use Class#getResource("/path/to/your/resource") where the path is the location from the "resource" folder
You can load the icon quite easily when it is in the resource folder of the class it belongs to at runtime.
See http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Within the class your.packagename.YourClassName you could write the following:
String folderName = YourClassName.class.getName().replace('.', '/' );
String urlString = "/"+folderName+"/RCar.gif";
URL fileUri = YourClassName.class.getResource(urlString);
button.setIcon(new ImageIcon(fileUri));
The urlString would be /your/packagename/YourClassName/RCar.gif. Of course this is also where icon should be at runtime.
HTH
yes...it takes the relative path.
public static final String FILE_PATH = "src/test/resources/car.png";
I am trying to read a file like :
FileInputStream fileInputStream = new FileInputStream("/com/test/Test.xml");
I am always getting the file not found exception. How can i make it work? Will the inputstream takes the relative path or not?
Yes, this can take relative path.
Why your expression does not work? Very simple: your path /com/test/Test.xml is absolute because it starts with /, so you are actually looking for file located in /com/test/ directory starting from root.
How to solve the problem?
I believe that you are trying to find file located under your project. So, you can use relative path like ./com/test/Test.xml or com/test/Test.xml. This will probably help. Probably because I do not know what is your current working directory and your file structure. Your current directory is where you are located when running java. If you are running from IDE typically the working directory is your project directory.
In this case I believe that path ./com/test/Test.xml is invalid because file Test.xml is not located directly under project root but somewhere under ./src/resources/com/test or so.
In this case probably you do not want to read the file as a file but as a resource (located into your classpath). In this case use
getClass().getResourceAsStream("/com/test/Test.xml")
Try using
class.getResourceAsStream(path). In that case, path will have to be relative to the folder containing the class invoking this statement.
InputStream in = getClass().getResourceAsStream("/com/test/Test.xml");
Try this,
String str = "Test.xml";
File file = new File(str);
String absolutePathOfFirstFile = file.getAbsolutePath();
FileInputStream fileInputStream = new FileInputStream(absolutePathOfFirstFile);
Your path must be incorrect. You can check the current directory by using System.getProperty("user.dir"); or printing the path of new File(".")
In my application, I would like to use a resource that exist in a folder media/src/main/resources/testMediaExif
To get that path, I used this piece of code, located in media/src/main/java/com/project/MyClass.java:
ClassPathResource resource = new ClassPathResource("classpath:testMediaExif");
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
The error shown is:
java.io.FileNotFoundException: class path resource [classpath:testMediaExif] cannot be resolved to URL because it does not exist
If I change that code:
ClassPathResource resource = new ClassPathResource("testMediaExif");
The variable absolutePath takes this value:
/Users/blanca/desarrollo/media/target/test-classes/testMediaExif
Why does it point to the target path? How could I change it?
There are two problems with new ClassPathResource("classpath:testMediaExif"):
The classpath: prefix is only used in config files (e.g. XML files), and should not be used if you're using ClasspathResource directly.
classpath:testMediaExif refers to a resource in the root of the classpath, not relative to the file in which you're making the reference.
Try this instead:
new ClasspathResource("testMediaExif", getClass())
or
new ClasspathResource("testMediaExif", MyClass.class)
These will construct a refernce to a resource called testMediaExif relative to MyClass.
One more thing: ClasspathResource.getFile() will only work in the resource really is a file. If it's packed in a JAR, then it won't work.
My guess is that the absolute path issue is because of the outputDirectory in the target of your maven POM . In my project the outputDirectory war/WEB-INF/classes and the it is from here the classes get executed . If I change it to some junk value , the class no longer gets executed .
So I believe the absolute path has to do something with the location of your .class files . Hope this helps .