Skip to content
Advertisement

Start javaw JFrame from BAT appearing behind File Explorer in Windows

I’ve had a batch file that for a long time has had no issues and started a JAR using javaw.exe in Windows, which then appears in front of other running Windows applications. Recently the batch file has been changed and I’m now consistently experiencing the JFrame, which is opened by the javaw.exe, to appear behind the File Explorer that was used to open the BAT file.

Specifically it appears on the task bar, blinking, but is not visible on screen because it is hidden behind the File Explorer that spawned it. It is not minimized.

Previously the batch was something along the lines of (somewhat simplified):

rem BOM safeguard
@echo off
set APPDIR=%~dp0
fc "%APPDIR%a.txt" "%APPDIR%b.txt" > fc1.txt
start "" "%APPDIR%..jrebinjavaw" -jar "%APPDIR%Testorama.jar"

Now it is something along the lines of:

rem BOM safeguard
@echo off
set APPDIR=%~dp0
fc "%APPDIR%a.txt" "%APPDIR%b.txt" > fc1.txt
fc "%APPDIR%a.txt" "%APPDIR%b.txt" > fc2.txt
xcopy /y "%APPDIR%Testorama.jar" "%APPDIR%Temp"
start "" "%APPDIR%..jrebinjavaw" -jar "%APPDIR%Testorama.jar"

To exclude the JAR being an issue I’ve replaced it, and it still occurs. Currently the JAR is a simple main:

package testorama;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

    public static void main(String[] args) {
        JFrame f = new TestFrame();
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(600, 600));

        f.add(p);
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

With the new bat above, I can comment out any of the fc-s or the xcopy and it will start being in front, but if all three are in there the problem occurs. At this point I have no clue why this inconsistency occurs. If I add a pause after the start it appears in front, but I do not want the prompt to be left open.

Why is this occuring? What can I do to mitigate the issue while maintaining all commands (as they have clear missions in running our application)?

My environment is a up-to-date Windows 10 and the javaw is JRE8. The problem occurs on other machines with a similar setup, while I haven’t tested other OSes or JREs.

Advertisement

Answer

Based on the input of @Mofi I ended up “solving” this by doing a minimal timeout command after running javaw.

My new BAT appearance is now:

rem BOM safeguard
@echo off
set APPDIR=%~dp0
fc "%APPDIR%a.txt" "%APPDIR%b.txt" > fc1.txt
fc "%APPDIR%a.txt" "%APPDIR%b.txt" > fc2.txt
xcopy /y "%APPDIR%Testorama.jar" "%APPDIR%Temp"
start "" "%APPDIR%..jrebinjavaw" -jar "%APPDIR%Testorama.jar"
timeout /T 0

I can only guess that this has to do with the existance of the parent prompt, at the time that javaw wants to display the JFrame, or some other obscure timing problem.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement