113

I'm trying to run a Java app (JAR file) on a high-DPI display on Windows 10. The app uses Swing and thus isn't DPI-aware. Normally when I run an application that isn't DPI-aware, Windows will scale it for me, albeit blurry. But for some reason it's not scaling the Java app; I just get a tiny little window in the corner with impossible-to-read text. How do I run a Java app with Windows' high-DPI (blurry) scaling?

12
  • Is it your application and do you control the sources/objects, or is it a legacy application that is now unchangeable ?
    – harrymc
    Nov 20, 2015 at 14:57
  • 4
    It's not my application; no control over the source.
    – Jeff
    Nov 20, 2015 at 18:53
  • 6
    @harrymc How would either of the above help? The problem isn't blurry fonts or that it refuses to run, the problem is that everything is tiny. The "blurry" font rendering is what I want to achieve, and as I understood the OP's question, they do too. Setting the DPI scaling to 100% would make the whole system tiny; setting the display resolution to a non-native one would make everything look blurry. (Okay, no blurrier than on a 1080p display for me, but still.)
    – millimoose
    Nov 25, 2015 at 20:48
  • 1
    @harrymc And while the latter might make the app usable, I'm sure you understand why answering "how do I make X work on a 2160p display?" with "use a worse display" is kind of missing the point.
    – millimoose
    Nov 25, 2015 at 20:49
  • 2
    I wouldn't blame Microsoft for this. Their solution for older desktop applications is the "blurry" upscaling, and it works, even though it's not pretty. The problem seems to be that Java "claims" it can handle a high-DPI display, or otherwise insists on working with physical pixels, as opposed to an upscaled abstraction, but then does is not quite able to cash the checks it's written. (At least for Swing applications.) That's why I was kind of hoping there's a solution on the Java side of things to make it stop pretending it can handle my display when it can't.
    – millimoose
    Nov 26, 2015 at 9:13

10 Answers 10

49

The problem here seems to be that Swing is by default claiming that it is DPI aware, so windows doesn't scale it. Use this switch to turn off this behavior and windows will start scaling your swing app:

-Dsun.java2d.dpiaware=false

[EDIT: Unfortunately, this flag no longer seems to work in Java 8, I was testing it in Java 6. Looks like this is a known issue.]

[EDIT 2: You can modify a Java 8 install to work correctly, using a program to modify the EXE manifests. I changed the setting from true to false in the manifests inside of java.exe and javaw.exe, and now my Swing programs scale correctly in Windows 10 high dpi. I used Resource Tuner to this.]

[Edit 3] Just use Java 9

13
  • Can you elaborate on the edit #2? Which setting did you modify in the manifest, exactly?
    – Tomalak
    Nov 11, 2016 at 14:56
  • 1
    In the Manifest XML, it is the <dpiAware> flag under <asmv3:application><asmv3:windowsSettings>
    – CarlG
    Nov 17, 2016 at 17:39
  • That's the point. I've seen that flag and I set it to false, but the application (JNLP/WebStart) did not scale properly either way. Can you think of a simple way to cross-check whether this works or not, e.g. a test app that you know of that responds to this setting?
    – Tomalak
    Nov 17, 2016 at 17:45
  • Webstart is particularly finicky because it can be hard to pinpoint exactly which executable in which jre distro you have installed you end up using. As a cross-check, I'd try launching your app directly with java.exe or javaw.exe and skipping JWS to see if there's a difference.
    – CarlG
    Dec 5, 2016 at 14:39
  • 6
    This is somewhere between comical and farcical. The framework mis-reports its capabilities and the switch that fixed the error is removed. The only work-around? For end-users to buy a 3rd party tool. And people wonder why Java has such a poor reputation.
    – Basic
    Aug 1, 2017 at 12:52
115

Just found an easy solution on my Windows 10 machine:

  1. Find java.exe you installed.
  2. Right click -> Properties
  3. Go to Compatibility tab
  4. Check Override high DPI scaling behavior.
  5. Choose System for Scaling performed by:
12
  • 5
    Best solution - I had this problem running a .jnlp file so I had to change the "C:\Program Files\Java\jre1.8.0_131\bin\jp2launcher.exe" but this worked like charm. May 18, 2017 at 13:49
  • 1
    For me it was the JDK version. Found by right clicking and opening the options from the task manager in Windows 10. May 31, 2017 at 18:32
  • 3
    For Windows 10, running Java-based Minecraft launchers, I had to change all the installed Java versions on my system for it to work (java.exe and javaw.exe).
    – cyberbit
    Jul 10, 2017 at 22:32
  • 6
    I used a less intrusive variant. I created a shortcut for command java -jar myJarFile.jar and did the steps 2 to 5 on that shortcut (Windows 10, Java 8). Aug 25, 2017 at 11:41
  • 5
    For me, it was javaw.exe
    – masterxilo
    Dec 13, 2017 at 11:28
49

If you stumbled across this question but are actually looking for a solution that works on Linux, this is for you.

If you can add parameters to the java binary which launches the application, you can use the option -D to pass a value for the sun.java2d.uiScale proprty to specify a scaling factor for Java2D. This will scale your application. The scaling factor value is a double. Make sure that you pass this option to the java binary itself, not the launched Java application.

Example: Launch NearInfinity.jar with a UI scaling factor of 2.5

java -Dsun.java2d.uiScale=2.5 -jar ~/jars/NearInfinity.jar

Alternatively, you can set the GDK_SCALE environment variable. Example:

GDK_SCALE=2 java -jar ~/jars/NearInfinity.jar

I found this ArchLinux Wiki article quite useful in general for running Linux on HiDPI systems, and some of the things might work on Windows as well.

10
  • 1
    This is good, though I wish I could get a scaling of 1 for JFrames and the system scaling for JOptionPanes.
    – NateS
    Aug 11, 2018 at 13:01
  • 1
    Doesn't work for me.
    – bkoodaa
    Sep 26, 2018 at 16:52
  • 1
    From what I can tell, uiScale must be an integer; I tried setting it to 1.5, but it had no effect; setting it to 2.0, however, did double the size. Running the Chatty twitch client on openjdk 14. Apr 13, 2020 at 15:27
  • 1
    This works for me. Thanks a lot. I am running this on Java 11. May 28, 2020 at 6:57
  • 1
    This did not work in my case using Java 8, but it did work using Java 14 (only tested these two versions)
    – tobias.h
    Jul 26, 2020 at 15:50
15

Solution: Run it on JRE 9.

This is because the Java runtime declared itself to be "DPI-aware" but didn't really supported it for AWT and Swing. Java applications were sized and rendered based on pixels rather than being properly scaled, this included HiDPI displays. Anyways, this has been recently solved. See the issue JEP 263: HiDPI Graphics on Windows and Linux and the upgrade.

So, increasing the font size does not work (because it does not increase the rest of the things); the jvm argument -Dsun.java2d.dpiaware=false does not work (because it is not really supported); and the manifest file + registry edit (for Windows) just does not work.

Then, You need to run it on JRE 9 because it really supports this feature.

12
  • I'll be really excited if Java9 fixes this. Aug 1, 2017 at 0:12
  • 4
    This fails to point out you have to recompile the code. Simply running the same Java 8- application on Java 9+ won't change the behavior
    – Ramhound
    Aug 1, 2017 at 1:25
  • 4
    @Ramhound You don't have to recompile. I just built a jar with JDK8 for Java 8 that overrides a fullscreen JPanel's paint() method on a W10 system with 4K screen and 200% scale setting. Run with the java.exe of Java 8, the Graphics2D object given to the method has a scaling of 1, and the JPanel has a getWidth/height of 3840x2160. Run with the java.exe of Java 9, the scaling is 2 and the width/height is 1920x1080. Oct 8, 2017 at 6:48
  • 1
    You'll find a new method in Java 9: Screen.getPrimary().getOutputScaleX(); which actually does work. (The other option is to use JavaFX screen size, (Screen.getPrimary().getVisualBounds()) which does work, and divide this against Toolkit.getDefaultToolkit().getScreenSize(), You'll find that you can get a guess at the DPI selected: 200% will result in about 2.0, but unfortunately 125% still results in a 1.0 Scaling percentage. However, even after moving to Java9: I found my GUIs all utterly destroyed as the buttons expanded, but some of the constants (used for gaps, padding etc). didn't. Dec 20, 2017 at 12:57
  • 2
    @wax_lyrical no need to do guesswork; the necessary API actually is there for a very long time. Get a GraphicsConfiguration, either using GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration() to get the default or just .getGraphicsConfiguration() on an actual, visible AWT Component. Then, you can invoke .getDefaultTransform() to get the transformation whose scaling component reflects the setting. If you need to deal with BufferedImage etc, needing pixels, apply the transformation to the size, then use identity for painting to screen.
    – Holger
    Mar 7, 2019 at 13:24
7

To force all java executables to have "properties > compatibility > dpi scaling mode" set to "System", in an administrator powershell (win-x, a), run:

$javaexes = (Get-ChildItem -path "$env:ProgramFiles\Java","${env:ProgramFiles(x86)}\java" -filter java?.exe -recurse  | Where-Object {$_.Name -match "java(|w).exe"} ).fullname

$javaexes | foreach {REG ADD "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"  /V $_ /T REG_SZ /D "~ DPIUNAWARE" /F}

to undo:

$javaexes | foreach {REG delete "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"  /V $_ /f}

Instead of HKCU you could use HKLM, but then you cannot change the dpi-scaling setting manually anymore in the properties > compatibility dialog of the java*.exe files.

4

You need to set PreferExternalManifest in regedit and create custom manifests for java.exe and javaw.exe as given in following stackoverflow answer https://stackoverflow.com/a/39372897

1
  • 4
    Good news, the Fall Creators update allows you to change scaling behaviour by right clicking on the item, selecting properties and then the compatibility tab. Creating a custom external manifest is no longer needed - which is nice.
    – Richard
    Dec 19, 2017 at 16:26
1

Elderry's solution is good, but what if you want to run a jar file on Windows and it doesn't have compatibility options?

Go to C:\Program Files (x86)\Java\jre\bin and find javaw.exe. Right click, properties, compatibility tab, check "override high DPI scaling behavior" and select "System" (Note that "System enhanced" did not work for me).

Now jar file windows should scale properly with readable text. If not, it might mean that Windows doesn't link jar type files to javaw.eve like it should. A third party fix is available at: https://johann.loefflmann.net/en/software/jarfix/index.html

1

Try Java 10.

I found that -Dsun.java2d.dpiaware=false does not work in Java 9 or 10. Also for Java 8 and 9 my swing app is small, but Java 10 sizes properly in windows!

so for an app that you can run from a bat file like: java -jar xxx.jar

I just added JAVA_HOME=....\java_10 and PATH=%JAVA_HOME%\bin;%PATH% in the bat file.

java -jar xxx.jar

1

Another possibility on Linux, albeit changing the UI more drastically: Forcing GTK and system fonts.

export _JAVA_OPTIONS="-Dswing.systemlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dswing.aatext=true -Dawt.useSystemAAFontSettings=on $_JAVA_OPTIONS"

It can also be applied to a single application:

_JAVA_OPTIONS="-Dswing.systemlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dswing.aatext=true -Dawt.useSystemAAFontSettings=on $_JAVA_OPTIONS" visualvm_216/bin/visualvm

Source: https://bbs.archlinux.org/viewtopic.php?id=185503#p1445438

-4

I was able to get sensible dpi when ran using command prompt like:

%SystemRoot%\system32\cmd.exe /C C:\your.file.path\yourJarFile.jar
1
  • This does not answer the question really, and it does not change any High DPI settings of java.exe or javaw.exe. Oct 14, 2019 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .