Json Relative Path - java

I use Java and libgdx for my application. I'm currently trying to describe a ui style with a json file, but I must give a path to a texture file. The absolute path is no problem, but how I can use a relative path especially go 2 folders back and then go through a few folders? I tried "../" but it doesn't seem to work.
Thanks for any help.

You go:
Gdx.files.internal("Path to project");
This will return a File that you can then use in your own ways. If you want to access the "root directory", which is the higher "level", put a / before the path, just like this:
Gdx.files.internal("/Path/ui.json");

Related

What path to put inside .getResource()

[Java]
I am having trouble figuring which String to put inside variable s below.
return new ImageIcon(getClass().getResource(s));
I do know that I need to put the path here, but I don't know the format.
This is the full path of the image I am trying to import (which didn't work):
Users/Kevin1031/eclipse-workspace/B-Tring/bin/textures/Center_Ring_0.png
I am using Mac OS, and B-Tring is the project folder, textures is the package, and Center_Ring_0.png is the image I want to import and use.
So, can someone tell me what path to put in?
first try the full absolute path with / at the beginning
/Users/Kevin1031/eclipse-workspace/B-Tring/bin/textures/Center_Ring_0.png
I don't know about Eclipse, I work in IntelliJ, where if you cofigure a folder as "resources" in the module settings, you can reference from there. f.e. if you would set bin/ as resourcer the path would be /textures/Center_Ring_0.png.
Third option is to use a relative path. f.e. if you call the method from B-Tring/src/foo.java try something like ../textures/Center_Ring_0.png. Notice not starting with the slash

Path, relative, direct

I know there were several similar questions, however, examples in them don't make things clear or I can't make profit of them - Shame on me.
So my problem is with loading images in simple app with GUI.
e.g.:
I got images in "D:\javaeclipseprog\Graphics\src\images", class and java files in "D:\javaeclipseprog\Graphics\src\app"
When I use direct path: "D:/javaeclipseprog/Graphics/src/images/icon.jpg" everything works, but as good practice I would like to get them from relative path, which as far as I know should be: "./images/icon.jpg".
Unfortunately it doesn't work.
Any help appreciated, thanks in advance.
When you are running it in eclipse, your default working directory in the project directory. That is the directory where srcis located in. In your example the project directory is:
D:/javaeclipseprog/Graphics
Therefore the correct path is:
./src/images/trophy.png
Edit: Just want to add that you could also load a file via a path relative to the class location by using the getResource method.
../../images/icon.jpg should work fine
You're going two folders up and go straight to the right folder.
Paths
A simple way to check this would be to use the Paths and Path classes and methods.
Path p1 = Paths.get("D:\\javaeclipseprog\\Graphics\\src\\app\\java.class");
Path p2 = Paths.get("..\\..\\images\\icon.jpg");
System.out.println(p1.resolve(p2).normalize()); // D:\javaeclipseprog\Graphics\src\images\icon.jpg
I'll use the inverted slash because it seems that you use windows. In this case .\ indicates that the same directory where the code is, will have the file you want to use. If you want to jump into the father of that directory, the one that contains the source, you'll use ..\
You can even do it more tan once, for example ..\..\ would be a valid path. Try adding quantities of ..\ in order to look for the directory you want. In this chase ..\src\images\icon.jpg (the parent on a java project is src)
Another important thing is that you're using / instead of \\ that would be the symbol of the directory separator on windows (\ is an special char that must be scaped using an aditional \) For portability i'd use:
String sep = System.getProperty("file.separator");
String path = ".."+sep+"src"+sep+"images"+sep+"icon.jpg"
I think what you need to use is "../images/icon.jpg".
Using it like you have it will look in the current directory, which unless I'm understanding it incorrectly, is "D:\javaeclipseprog\Graphics\src\app".

Can I use relative URL in a .properties file?

I have a java program which gets some properties from a xxx.properties file. For example the destination of a file my program works with.
How is it possible to give this file's place in the xxx.properties file with relative linking? I tried so many ways, but nothing worked. If I give the place of the file with an absolute URL it works just fine.
Example:
keyFileName=../res/MP00.pem <-- does not work.
keyFileName=/home/thomas/myprogram/src/main/webapp/WEB-INF/res/MP00.pem <-- does work.
The xxx.properties file is in /home/thomas/myprogram/src/main/webapp/WEB-INF/lib
I'm using an ubuntu based linux distribution, if that matters.
Any idea? Thanks in advance!
This has little to do with the fact that you load the URL from a properties file. Relative paths are always relative to some form of 'current location'. Loading the URL-String from a properties-file does not set that .properties' location as your 'current location'. Try setting the path relative to the program you run (which uses the URL-String), not the .properties-file.

Saving a file in Windows

I have a folder called WalnutiQ. Inside this folder is is a file at WalnutiQ/train/model/MARK_II/Save.java
Save.java
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"Digits.txt");
which works! However, the file Digits.txt is unfortunately saved in WalnutiQ/Digits.txt
How do I save the file Digits.txt at WalnutiQ/train/model/MARK_II/Digits.txt???
I am programming in java in eclipse in windows. I have tried
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/train/model/MARK_II/Digits.txt");
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"\\train\\model\\MARK_II\\Digits.txt");
but neither work.
judging from the result you are getting your current directory is pointed at WalnutIQ. You might try using .\train\model\MARK_II\Digits.txt. Windows treats the . (period) as a token for "current directory". Your other attempt would have tried to find the train directory in the root of C because the \ (backslash) is a token for the root (c:). It likely fails because that folder does not exist - unless it created it... might go look :) I don't use Json in eclipse which is why I'm not answer your question with code.
Have you considered using the full absolute file path to the text file, rather than the relative one you are currently using? This may not be practical, depending on how you plan on using your program, but it might be worth a shot.
Depending on how the library saves files (I imagine it's using File behind the scenes), you should be able to supply an absolute path to the location you want it to save to.
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/path/to/WalnutiQ/train/model/MARK_II/Digits.txt");

Using links in different folders for images (Java)

I'm designing a calculator with customised buttons. Naturally I want to organise my image files in folders different to the package interfaces.
The location of the folders is interfaces/Seven/seven-normal.png but whenever I don't include the full link
"C:\Liloka\Source\interfaces\Seven\seven-normal.png"
it doesn't work and everything disappears. I swear I have seen this being done in normal code. If I used this in proper programs I can't expect people to be changing the link to where they've put the code! Here's the code I've been using:
seven = new ButtonImage("/Seven/seven-normal.png"); - Doesn't work
nine = new ButtonImage("C:/Users/Liloka/workspace/WebsiteContent/src/interfaces/Nine/nine-normal.png"); - Does work
Thanks!
"/Seven/seven-normal.png"
...is a path to C:\Seven\seven-normal.png - because of the / at the very beginning of your path, which essentially means, from the root of the drive, go to the "Seven" folder, and then load "seven-normal.png"
You have to use a relative path, something like, "../../interfaces/Seven/seven-normal.png" or maybe just "interfaces/Seven/seven-normal.png"
The first path will take you "up" two folders, and then down to interfaces/Seven/seven-normal.png. Essentially, you have to figure out what folder your code is running under, also called the "working directory", and construct a relative path from there.
Just delete the first forward slash.
seven = new ButtonImage("interfaces/Seven/seven-normal.png");
interfaces is the folder that should be in the same folder as your JAR.

Categories