95

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?

  • Is it your application and do you control the sources/objects, or is it a legacy application that is now unchangeable ? – harrymc Nov 20 '15 at 14:57
  • 3
    It's not my application; no control over the source. – Jeff Nov 20 '15 at 18:53
  • 5
    @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 '15 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 '15 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 '15 at 9:13

10 Answers 10

41

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

| improve this answer | |
  • Can you elaborate on the edit #2? Which setting did you modify in the manifest, exactly? – Tomalak Nov 11 '16 at 14:56
  • 1
    In the Manifest XML, it is the <dpiAware> flag under <asmv3:application><asmv3:windowsSettings> – CarlG Nov 17 '16 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 '16 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 '16 at 14:39
  • 4
    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 '17 at 12:52
92

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:
| improve this answer | |
  • 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. – Dror Harari May 18 '17 at 13:49
  • For me it was the JDK version. Found by right clicking and opening the options from the task manager in Windows 10. – Brian Knoblauch May 31 '17 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 '17 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). – Julien Kronegg Aug 25 '17 at 11:41
  • 4
    For me, it was javaw.exe – masterxilo Dec 13 '17 at 11:28
27

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.

| improve this answer | |
  • This is good, though I wish I could get a scaling of 1 for JFrames and the system scaling for JOptionPanes. – NateS Aug 11 '18 at 13:01
  • 1
    Doesn't work for me. – Atte Juvonen Sep 26 '18 at 16:52
  • @AtteJuvonen Instead of just telling "my car doesn't work", when it doesn't work for you, why don't you post a new question describing what you've tried, what was the expected result, what was the actual result, what command line you've used, and what, if any, error messages you got? – Christian Hujer Sep 27 '18 at 5:51
  • 1
    This works for me. Thanks a lot. I am running this on Java 11. – thisisshantzz May 28 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 at 15:50
11

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.

| improve this answer | |
  • I'll be really excited if Java9 fixes this. – music2myear Aug 1 '17 at 0:12
  • 3
    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 '17 at 1:25
  • Running HiDPI programs on Java 9 solves a lot of problems. goo.gl/3zmL7b – Gernot Sep 23 '17 at 17:45
  • 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. – Dreamspace President Oct 8 '17 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. – wax_lyrical Dec 20 '17 at 12:57
5

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.

| improve this answer | |
2

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

| improve this answer | |
  • 3
    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 '17 at 16:26
0

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

| improve this answer | |
0

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

| improve this answer | |
-2

I tried most of these solutions, but the one that worked for me was setting the system compatibility mode on the files I found in the following location:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath
| improve this answer | |
  • 2
    Welcome to Super User! Please don't add "thanks" or "this one worked for me" as answers. Invest some time in the site and you will gain sufficient privileges to upvote answers you like, which is the Super User way of saying thank you. – bertieb Nov 30 '18 at 15:11
  • This could be a comment on someone else's answer. To fix, it's necessary to apply compatibility settings to java.exe and/or javaw.exe or use other recommendations. The javapath folder contains links to latest Java executables installed on the system. Thus applying the settings in Program Files should be enough. – Alexey Ivanov Oct 14 '19 at 17:20
-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
| improve this answer | |
  • This does not answer the question really, and it does not change any High DPI settings of java.exe or javaw.exe. – Alexey Ivanov Oct 14 '19 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.