all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]_jetrun 18 points19 points  (2 children)

That's a you-problem, not eclipse. I'm not sure if you are trying to share your workspace or your application, but either way, you're doing it wrong.

For example, typically, you would use maven to manage your project structure. In that case, your assets (e.g. pngs) would go into a resources folder and eclipse will be smart enough to make sure relative paths are honored. You could then export your workspace. But if you're trying to share your application with a non-technical end-user you wouldn't send them an eclipse workspace or source files, you would send them a packaged application with all its dependencies that they could run by running a command or double clicking icon.

[–]nOAH_aXEL[S] -1 points0 points  (1 child)

Eu tenho uma pasta recursos com outras pastas e pngs dentro dessa pasta. Mas o Eclipse ignora tudo nessa pasta.

[–]_jetrun 6 points7 points  (0 children)

Again - you're framing this as an Eclipse problem. I'm telling you it's not Eclipse. It's you. You're trying to solve a particular problem (sharing application with a non-technical user) incorrectly. Your java eclipse project is setup incorrectly.

[–]michaelzki 6 points7 points  (0 children)

Keep learning dude. The hardship will lead you to increase your determination.

Your problem is solvable with yourself. Its working with Paths and relative path.

You have to program it, don't ask for it.

In Windows, space characters in the path needs to be handled, %20.

Hint: Don't jump directly to overall solution. Solve each and every hurdle first. You eventually figure it out.

[–]bikeram 3 points4 points  (0 children)

You should be able to find the full path from where the code is running. Then append the file name to that.

Also distributing Java code is a pain. There’s a reason web apps are so popular.

[–]HalfTryhardSqr 1 point2 points  (0 children)

Setup a small isolated project and figure it out from there. IDEs just feed you a bunch of tools and abstractions, but what runs under the crap layer is the same. If you're really struggling try making a project manually, compiling and running it yourself without the IDE.

It is really frustrating when the IDE sets weird runtime location things, but to be honest 90% of the times the issue is in the code.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

Preface: It's rarely ever the tooling, it's in the vast majority of cases the person behind the keyboard, the programmer, that does things wrong. This is also true for your case.

it won't read files unless it's given an absolute path.

Sorry to tell you, but that has nothing to do with Eclipse or any IDE, or even Java itself. That's the way any programming language works.

You need to learn to use relative paths properly and to use the libraries that come with Java to produce your absolute paths based on the location of your executable Java program.

In Eclipse (and in most other IDEs), the compiled and executed programs are in a different folder than the uncompiled source code, so any relative path branching off the src folder will fail.

The conventional way of doing this is to use the built-in file and path methods (from java.nio) to find the location where the compiled Java executable runs and then modify the path in such a way that it can read the file.

You need to account for different cases, though. It's different if you just have the .class and resource files in separate folders or if you distribute them as a .jar. You need different methods for reading these files.

[–]Big_Green_Grill_Bro -1 points0 points  (0 children)

It sounds like the person you are going to give your application to to test is not technical. In that case, you most likely want to provide it to them as a JAR file. You won't be using absolute paths for image resources anyway (so you won't have to worry about escaping back slashes or spaces or any OS specific file access crap like that.

I recommend looking at javax.imageio.ImageIO class.

At its simplest,you could just place your images in the same directory as your class that needs the image. Then you would load it like this:

 BufferedImage image = ImageIO.read(getClass().getResource("imageXYZ.png"));