2018 MacBook Pro experiencing very poor battery life (<4 hours) with light usage
Considering your updated question that your problem is that running a Java-program forces the discrete GPU active thus draining your battery:

This is a known bug with some older versions of Java and JavaFX. Upgrade your Java to the latest version to see if that fixes this for you (see java bug JDK-8132775 and JDK-8041900).

If that still does not fix it, find the offending application and open its application bundle. Inside you'll find a Info.plist file, which you'll want to edit to add the key:

NSSupportsAutomaticGraphicsSwitching


the key should be a boolean set to true.

This should fix the problem if the application uses a sufficiently new version of Java.

If you're not confident doing this, report the GPU behavior as a bug to app developer and hint them about NSSupportsAutomaticGraphicsSwitching. Hopefully they will put out an update fixing the problem.

If your Java program does not come as a .app but instead is a bare jar-file, you'll want to convert it to an app first. Use the jar2app script to convert it to an app, and then use the instructions above to add the NSSupportsAutomaticsGraphicsSwitching key. Download jar2app from here:

https://github.com/Jorl17/jar2app