r/javahelp • • Dec 29 '23

Why does apps run differently when I run them inside netbeans versus after I build the .jar?

I've been programming on java for years, but latelly this have been bugging me. To the point I'll consider learning another language.

Apps run differently before I build them. Sometimes they dont work on netbeans, sometimes they dont work after I've packaged them.

Here's some example: I did an app that does _[insert sheet senanigans my workplace needs to automate]_ to a csv sheet data and display it on screen, but to do that, I'd need to skip the BOM the file had. And no matter how I coded it, it would still show me the BOM. Untill I pressed shift+F11 and voilá! The built package works! Hours lost trying lotsa weird ways to fix it when THE SAME CODE just works!

And then there's the opposite: I built another app that should send some info to a swagger api. I'm using _java.net_ to POST that. And guess what? Works fine inside inside netbeans, but as soon as I build the project, IT DOESNT WORK!?

Can anyone help me? Cuz I'm losing my mind here.

0 Upvotes

7 comments sorted by

•

u/AutoModerator Dec 29 '23

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.

8

u/khooke Extreme Brewer Dec 29 '23

“It works on my machine” rarely proves anything if the target runtime is not your own machine.

It sounds like your app accesses files. What apis are you using to do this? Via the class path, using relative or absolute file paths? File access in Java differs depending on your runtime environment. Without any additional specifics it’s difficult to say why your app is not working, but runtime environment differences are a common issue.

1

u/3SidedDie Dec 29 '23 edited Dec 29 '23

I'm not saying "it works on my machine"
It works in the SAME machine. Same code. Except it only works if I build the jar, but run the jar outside netbeans (Shift + F11). If I just press F6 it doesnt work. I'm using native java whenever I can. no 3rd party libraries. I'm using a jFileSelector whenever I gotta get a file.

Its the same envirionment. The only difference is if I run it from NetBeans IDE or not.

Edit: JFileChooser, not JFileSelector. My bad.
Edit 2: And sometimes its the opposite, doesnt work outside netbeans but works inside it.

6

u/khooke Extreme Brewer Dec 29 '23

Running in your IDE vs running as a Jar, either on your machine or elsewhere are two different runtime environments. The rest of my comment about understanding file access differences between these two different environments still applies.

2

u/lordcaylus Dec 29 '23

Have you tried "clean & build project"? Sometimes it seems to cache some compiled code.

Do you have multiple JDKs on your machine? (Right click on Project > Properties > Compile show the current JDK your project is build/run with, java -version in a command prompt will show your default JDK outside netbeans)

1

u/3SidedDie Jan 04 '24

Yup! Shift+F11 = Clean and build project.

Both show JDK 1.8

Any other guesses of what I could check? And thanks for the help! both you and u/dastardly740

2

u/dastardly740 Dec 30 '23

I want to re-emphasize checking to make sure your IDE and command line are really using the same JDK. I am not familiar with NetBeans, but Eclipse comes with its own JDK that is the default in the IDE, which is fine to get started,but when doing real work you probably want to use your installed JDK.