Search code examples
javajarjframefullscreenscreen-resolution

Change Screen Resolution of Remote Session using Java RunnableJar


Changing Screen resolution of current system is working fine, but i need to change the screen resolution of remote session (it may be in virtualbox). Which is connected and through program i need to change it. Form webbased application i will call the runnable jar file by suppling parameters.

public static void main(String[] args) throws InterruptedException, IOException {
    screenResolution(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]));   // Screen Resolution of Remote Session.
}

public static void screenResolution(int width, int height, int bitDepth, int refreshRate) throws InterruptedException{
    java.awt.GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    java.awt.DisplayMode change_Mode = new DisplayMode(width, height, bitDepth, refreshRate);//DisplayMode.REFRESH_RATE_UNKNOWN);
    DisplayMode[] supportedResolutions = graphicsDevice.getDisplayModes();
    System.out.println(supportedResolutions.length);
    for (DisplayMode resolution : supportedResolutions) {
        System.out.format("%d by %d, (%dbit), %d Hertz \n", resolution.getWidth(), resolution.getHeight(), resolution.getBitDepth(), resolution.getRefreshRate());
        if (resolution.equals(change_Mode)) {
            System.out.format("supported = %s\n",resolution);
            DisplayMode current_displayMode = graphicsDevice.getDisplayMode();
            if (change_Mode.equals(current_displayMode)) 
                System.out.println("=== No need to change Resolution ===");
            else {
                java.awt.GraphicsConfiguration graphConfig = graphicsDevice.getDefaultConfiguration();
                javax.swing.JFrame frame = new JFrame("",graphConfig);
                setResolution(graphicsDevice, frame, change_Mode);
            }
            break;
        }
    }
}
public static void setResolution(GraphicsDevice graphicsDevice, JFrame frame, DisplayMode displayMode) throws InterruptedException{
    try {
        graphicsDevice.setFullScreenWindow(frame);
        graphicsDevice.setDisplayMode(displayMode);
        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); 

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println("After Cahange Resolution : "+screenSize.getWidth()+" * "+screenSize.getHeight());

        // doing other work......
        Thread.sleep(1*60*1000); // min sec millisec
    }catch(IllegalArgumentException e){
        e.printStackTrace();
    }finally{
        frame.dispose(); //program terminates. screen resolution changes to previous resolution.
    }
} 

running my jar file in current machine from console D:\JarLocation>java -jar ChangeResolution.jar 1366 768 32 60 it works fine. but i need to change the resolution of screen when ever client what different resolutions to test his senario.

I have run the same jar file in remote machine but it cannot change.

Even mannually also i cannot change it. It say's Display settings cannot be changed.


Solution

  • RDP - Connect to the administrative session of the remote computer

    enter image description here

    capthNode for yash.rdp ► someip.rdp

    through connecting this way client / our-java program can change the appearance of 'DISPLAY'