Check if the program is running on Windows

Asked

Viewed 1,391 times

-2

Good night.

I was wondering if you could give me a JAVA example of how to check if my program JAVA-MADE is running on Windows.

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

In short, I would like to check if the user has run the program JAVA-MADE in windows and if yes run the command JAVA "chcp 65001".

  • Lucas, there is no doubt regarding java in your question. This tag makes no sense in it.

  • I put Java, because I want an example in Java.

2 answers

3

I answered that question when I had the tag .

This code checks if the Notepad is open, if it is running 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
  • Thanks. But I would like something in Java. In this case your code doesn’t help me much, but thank you so much for trying to help!

  • @Lucas saves in a . bat and executes via Runtime.exec(). Otherwise it is not possible to do this natively in java.

  • 4

    You had put the tag cmd...

  • Laérciolopes was @Articuno who changed the tag, but thank you so much anyway!

  • 1

    @Lucas changed because the doubt was applicable to the tag, his question is that it is not very clear. The answer meets very well what is written in the question, if it is not well your doubt, edit and be more specific in the explanation.

  • @More clear than that article? oo What was your doubt? Anyway, the doubt has been cleared up, you said it was not possible to do this, but Victor showed you how to do it. Anyway thank you.

Show 1 more comment

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 his way out on my laptop:

Windows 8.1
6.3
amd64

I put him in the ideone. Here’s the way out there:

Linux
3.16.0-4-amd64
amd64

To execute the chcp 65001, you can use the method 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");
        }
    }
}
  • Putz Victor. Ball show. Really worth!

  • Victor. Would it be possible for me to check if the cmd is open and to execute the command only if the cmd is open? I’m doing some exercises and the teacher will fix it, but now I think he may be using some IDE or text editor with a terminal plugin like Vscode for example, to test the code. In this case the command becomes useless.

  • @Lucas This is much harder. I recommend in this case you simply ask the teacher where it will run this. Surely the point of the exercise is not to keep bumping your head on how to show accented characters on the way out, so you shouldn’t even be worrying too much about it.

  • Actually I will make a test to be dispensing some materials for efficiency, one of the activities is to build programs of organizational level. I come from C# and some things in Java are still vacant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.