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?
9 Answers
Just found an easy solution on my Windows 10 machine:
- Find
java.exeyou installed. - Right click ->
Properties - Go to
Compatibilitytab - Check
Override high DPI scaling behavior. - Choose
SystemforScaling performed by:
- 1,861
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.
- 699
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
- 636
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.
- 271
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.
- 537
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
- 41
- 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
- 131
- 3
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
- 11
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
- 111