No such file or directory Eclipse Java Mac OSX - java

I keep getting this error every time I run my OpenGL program and try and set up a texture:
Unable to read texture file: java.io.FileNotFoundException: bricks.gif (No such file or directory)
I have the file bricks.gif in the same folder and it shows up in Eclipse as well, but I have no idea what is going on. I tried to clean and refresh as well, but nothing.
Any help would be greatly appreciated.

Two ideas for solving your problem:
Make the reference to bricks.gif absolute, adding the full path.
(e.g. /Users/abc/workspace/bricks.gif)
When Eclipse starts your Application, the directory root will be your project folder.
Think of the following layout in Eclipse Package Explorer:
projectname
-src
-bin
-...
-bricks.gif
You can refer to the file in relative way by using the path "./bricks.gif". You can obtain a File Object pointing to bricks.gif with the following code:
File bricks = new File("./bricks.gif");
or
File bricks = new File("bricks.gif");
Cheers

Related

Gradle - Accessing files outside the project

This is a very simple question really but I am having to pull my hair over it.
In Gradle, I am having to copy some files in my project to a location outside the project - /var/tmp/a_particular_folder/
The target path will remain the same on both Windows and Linux boxes.
So my task looks like this:
task copyFilesNeededForTests(type: Copy) {
from 'src/testconfiguration/'
into '/var/tmp/a_particular_folder'
}
But that does not work! It copies the files relative to the root project's path.
I've tried a lot of things:
into new File('/var/temp/my_particular_folder') // creates relative to project root
into file ('/var/temp/my_particular_folder') // again relative to project root
into '/var/temp/my_particular_folder' // again relative to project root
into '//var/temp/my_particular_folder' // Throws nullpointer
into 'c:/var/temp/my_particular_folder' // works but only on windows.
Any clues anybody of how to copy into a filesystem folder relative to the root of the filesystem?
Edit:
Using version 2.8
Answering myself.
I managed to solve this problem by using:
into new File('/var/temp/my_particular_folder').absolutePath
Note the 'new File()' and then 'absolutePath' on it.
Works on Windows, Linux, Mac.

Loading image from a Java file

I have this project structure:
ProjectName
src
xxx
co
com
package
something.java
web
image
print
pic.jpg
I want to load the pic.jpg in the java file which will then be used in the pdf file generated. I have gone through the answers here but, nothing helped me yet. Possibly I am missing a small thing.
If the pic.jpg was under package, then getClass().getResource("pic.jpg") works absolutely fine. getClass().getResource("/web/image/print/pic.jpg") doesnt work as well. But I want to place all my images under image folder and refer it in the java file.
You should get the application runtime path and rebase the path to the folder which images locate.
You can use the code:
String path = new File(".").getCanonicalPath();// or System.getProperty("user.dir")
Seems your files are not copied to bin/ directory. Change your build script to copy them to output directory
getClass().getResource() will use class loader to load class, since those files need to be in classpath

Issue file assets Libgdx

I have a problem to load a file from assets.
In pratcise I have to load TiledMap file and I do it in this way:
arrayTiledMap.add(new TmxMapLoader().load(Gdx.files.internal("scenario.tmx").path())));
(I add it in array for other reason)
In the project the tmx file(scenario.tmx) is located in the android assets folder.
When I execute the program in Eclipse there aren't problems , but when I create the JAR file for Desktop project and I execute it I get this error on console(I launch it by terminal):
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: Documents/University/Programming/Street.png (Internal)
The file Street.png is a file that I used in tiledMap editor for create the map.
At this point I check the JAR file contents and In the root directory of JAR there is the Documents folder and in it there is University folder and so on.
Because if the path is in the JAR file I get this error?
What is that wrong?
Thank you very much for your time, this error is driving me crazy
If you need other code, in particular, tell me.
(although I do not think since the problem only occurs when loading the file)
Because if the path is in the JAR file I get this error?
No. Its most likely you aren't exporting the JAR correctly.
File -> Export -> Java -> Runnable JAR File
And be sure to check this:
package required libraries into generated JAR.

Java FileInputStream can't find file (LibGDX)

So I have this class:
http://pastebin.com/EwXFwuZz
And this directory tree:
http://s14.directupload.net/file/d/3099/uskko5mo_png.htm
And I'm working with the LibGDX Framework on this project. This is basically my problem:
I have a file that contains level information in "chunks". Each line is one chunk. I want to read the file line per line. Unfortunately the built in FileHandling system of LibGDX doesn't support line by line reading so I thought to stick to the stock java one.
However I'm getting this "FileNotFound" Exception:
java.io.FileNotFoundException: ./assets/data/lvls/example.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at gemini.cute.game.xvii.database.LevelReader.<init>(LevelReader.java:49)
at gemini.cute.game.xvii.core.MainLauncher.create(MainLauncher.java:40)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:124)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:107)
With LibGDX the file is found but read into ONE single gigantic string. And for some reason with the same path (even going from the root) the file isn't found.
For people not familiar with LibGDX I'm coding in the upper "CuteGameXVII" project but for compilation I run the "Main" in "CuteGameXVII-desktop". The assets folders are linked via eclipse and worked for other resources so far.
Am I missing something super obvious here? If so, please help me :P Thank you in advance.
If you're running the Java program from a directory with path $DIR, the input file should be at $DIR/assets/data/lvls/example.txt. Based on the exception that you've received, the input file doesn't exist at this location.
I'd suggest that you first try using the absolute path to the input file in your code. Then, figure out what the relative path to it is.
I experienced this issue too. In order to read a file from your asset directory with LibDGX you must use the LibGDX method replacing:
new FileInputStream("SomeFile.txt")
by
Gdx.files.internal("SomeFile.txt").read()
assuming "someFile.txt" is in your asset root folder.

Find a resource both in .jar and eclipse with the same String computation

I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.

Categories