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".
Related
[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
I am trying to use a relative path to locate an executable file within a Java class instead of hard-coded lines which worked, but using something like:
final static String directory = "../../../ggla/samples/obj/linux_x86"
fails... what is the proper way of using a relative path in Java?
The most likely explanation is that your current directory is not where you think that it is. You can inspect the system property of user.dir to see what the base path of the application is, or you can do something like this:
System.out.println(new File(".").getCanonicalPath());
right before you use that relative path to debug where your relative reference starts.
Use System.out.println(System.getProperty("user.dir")); to see where your current directory is. You can then use relative paths from this address.
Alternatively if you dont need to update the file you are trying to read obtain it from the classpath using getResourceAsStream("filename");
Karl
What you need is getCanonicalPath or getCanonicalFile to resolve the relative paths.
System.out.println(new File("../../../ggla/samples/obj/linux_x86")
.getCanonicalPath());
You can use relative paths like you describe, and it should work. So the error is probably somewhere else.
Please post a complete program to reproduce your error, then we may be able to help.
I would start by using the separator and path separator specified in the Java File class. However, as I said in my comment, I need more info than "it fails" to be of help.
For relative path, maybe you can use one of the method provide by java for the beginning of the relative path as getRelativePath....
You have to use / and not // in the String in java !
Due to a project requirement I had to move from Selenium 2.0 to Watir and Cucumber framework. Earlier we were using Java and often set the default file path as:
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/browsers/chromedriver.exe");
Now I am trying to set up something similar in Ruby also, but being new to Ruby, I'm failing to get the root directory of the project. It only gets the path of the file which the current user is working using __FILE__ or Dir.pwd.
My Watir project structure is like this:
root
-config
-features
-pages
-step_definitions
-support
-hooks.rb
-browsers
...
...
I want to specify in hooks.rb that if the parameter passed is "Chrome", then run the Chrome browser, and when it is "FF" run the Firefox browser, but it always gives me the current working directory path and I need the root directory.
I am on WINDOWS 7, Using Ruby version 1.9.3p551.
Ruby's File class has several different methods that are useful for what you're trying to do, so read the documentation for absolute_path, expand_path, realdirpath and realpath.
I'd recommend starting with absolute_path:
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~” it is NOT expanded, it is treated as a normal directory name.
I built the path from your hooks.rb to the root directory in my tests directory on my Desktop, and added this code to hooks.rb:
PATH_TO_ROOT = File.absolute_path('../..', File.dirname(__FILE__))
Running it shows:
PATH_TO_ROOT # => "/Users/ttm/Desktop/tests/ruby/root"
You'll always have to maintain the '../..' relative string but that is easier than hard-wiring your code with a fixed absolute path.
I tend to put code similar to this in any project that has multiple directories, especially when I'm calling library files. Write your code correctly, and you can do it once assigning the value to a constant, and you can then reference that constant wherever you need to know the absolute path to the installation.
book = Roo::Excel.new(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"config/data/test.xls"))
It's a lot easier than that:
path_to_book = File.absolute_path('../../config/data/test.xls', File.dirname(__FILE__))
path_to_book # => "/Users/ttm/Desktop/tests/ruby/root/config/data/test.xls"
File.dirname(__FILE__) is the anchor for your relative path. Simply define the relative path to the file you want and let Ruby do the rest.
"Relative path to your project directory" talks about relative pathing in Ruby.
The answer to your question is going to be specific to the code that you are trying to use in your hooks.rb. If you could post a sample of what code you are using for this from your hooks.rb, that would be great. But the answer to your question is likely going to be some combination of the following:
File.join(File.dirname(__FILE__), '../../')
Where the first parameter is your current directory (root\features\support) and the second parameter is going to be the relative path to your root directory ('../../').
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");
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.