Verify that the program is running in Windows [closed]

-2

Good evening.

I would like to know if you could give me an example in JAVA of how to check if my MADE IN JAVA program is running on Windows.

Taking advantage of the topic, I would also like to know how I execute a command in CMD USING JAVA in case it is running the program in Windows. It is common that windows cmd does not recognize accents, but simply type the command " chcp 65001 " that it recognizes ...

In short, I would like to check if the user has run the MADE JAVA program on windows and if so run the EM JAVA command. >     

asked by anonymous 02.10.2017 / 01:45

2 answers

2

Just use the system properties os.name , os.version and os.arch . They are documented here .

See a very simple code:

class Teste {
    public static void main(String[] args) {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("os.arch"));
    }
}

Here's the output from it on my notebook:

Windows 8.1
6.3
amd64

I also put it in the ideone . Here's the output there:

Linux
3.16.0-4-amd64
amd64

To run chcp 65001 , you can use the Runtime.exec(String) ":

import java.io.IOException;

public class TrocaAcentos {
    public static void main(String[] args) throws IOException {
        if (System.getProperty("os.name").contains("Windows")) {
            Runtime.getRuntime().exec("C:\Windows\System32\cmd.exe /k chcp 65001");
        }
    }
}
    
02.10.2017 / 08:39
2

This code checks whether the notepad is open, if it executes the command chcp 65001 :

@echo off
set programa=notepad.exe

tasklist /FI "IMAGENAME eq notepad.exe" 2>NUL | find /I /N "notepad.exe">NUL
if "%ERRORLEVEL%"=="0" (
    echo O %programa% esta aberto.
    chcp 65001
) else (
    echo O %programa% nao esta aberto.
)

pause > nul
    
02.10.2017 / 02:55