How to change command prompt (console) win

2019-01-15 08:09发布

How to change and update the title of the command prompt window from the java command line application? Every time I run my application, the command prompt window title shows: C:\WINDOWS\system32\cmd.exe - java MyApp.

I'd like to change and update the window title as the java program runs, for example as wget(win32) updates downloading status in the title: Wget [12%].

4条回答
干净又极端
2楼-- · 2019-01-15 08:43

This depends on your terminal emulator, but essentially it's just printing out control sequences to the console.

Now I'm not clear on what control sequences CMD.EXE responds to (I haven't one available to try this on) but I hear there's a command called TITLE which sets the title of the window. I tried piping TITLE's output to a file, but apparently, it doesn't actually set the title by outputting control characters. The START command can take a parameter which is title of the window followed by the command to run in the window. So something like

cmd TITLE "lovely Application that is in a command window." && "java" MyApp
REM or
start "lovely Application that is java based." java MyApp

Personally I would just bundle the whole thing with a shortcut where you can edit the properties such as the current directory, the command, it's parameters, and the window size, style and title (if I remember rightly). Give it a nice icon and people will use it.

查看更多
Root(大扎)
3楼-- · 2019-01-15 08:44

Here's my solution using JNA:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class SetTitle {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)
            Native.loadLibrary((Platform.isWindows() ? "kernel32" : "c"),
                               CLibrary.class);

        boolean SetConsoleTitleA(String title);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.SetConsoleTitleA("Testing 123");
        System.exit(0);
    }
}
查看更多
贼婆χ
4楼-- · 2019-01-15 08:53

Although I haven't tried it myself, in Windows, one can use the Win32 API call to SetConsoleTitle in order to change the title of the console.

However, since this is a call to a native library, it will require the use of something like Java Native Interface (JNI) in order to make the call, and this will only work on Windows 2000 and later.

Edit - A solution using JNI

The following is an example of using JNI in order to change the title of the console window from Java in Windows. To implement this, the prerequiste is some knowledge in C and using the compiler/linker.

First, here's result:

Changing the console title from a Java application http://coobird.net/img/jni-change-console-title.png

Disclaimer: This is my first Java application using JNI, so it's probably not going to be a good example of how to use it -- I don't perform any error-checking at all, and I may be missing some details.

The Java program was the following:

class ChangeTitle {

    private static native void setTitle(String s);

    static {
        System.loadLibrary("ChangeTitle");
    }

    public static void main(String[] args) throws Exception {

        for (int i = 0; i < 5; i++) {
            String title = "Hello! " + i;
            System.out.println("Setting title to: " + title);
            setTitle(title);
            Thread.sleep(1000);
        }
    }
}

Basically, the title is changed every 5 seconds by calling the setTitle native method in an external native library called ChangeTitle.

Once the above code is compiled to make a ChangeTitle.class file, the javah command is used to create a C header that is used when creating the C library.

Writing the native library

Writing the library will involve writing the C source code against the C header file generated by javah.

The ChangeTitle.h header was the following:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ChangeTitle */

#ifndef _Included_ChangeTitle
#define _Included_ChangeTitle
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     ChangeTitle
 * Method:    setTitle
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
  (JNIEnv *, jclass, jstring);

#ifdef __cplusplus
}
#endif
#endif

Now, the implementation, ChangeTitle.c:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <jni.h>
#include "ChangeTitle.h"

JNIEXPORT void JNICALL
Java_ChangeTitle_setTitle(JNIEnv* env, jclass c, jstring s) {
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, s, NULL);

    SetConsoleTitle(str);

    (*env)->ReleaseStringUTFChars(env, s, str);
};

A String that is passed into the native function is changed into an UTF-8 encoded C string, which is sent to the SetConsoleTitle function, which, as the function name suggests, changes the title of the console.

(Note: There may be some issues with just passing in the string into the SetConsoleTitle function, but according to the documentation, it does accept Unicode as well. I'm not too sure how well the code above will work when sending in various strings.)

The above is basically a combination of sample code obtained from Section 3.2: Accessing Strings of The Java Native Interface Programmer's Guide and Specification, and the SetConsoleTitle Function page from MSDN.

For a more involved sample code with error-checking, please see the Section 3.2: Accessing Strings and SetConsoleTitle Function pages.

Building the DLL

The part that turned out to take the most amount of time for me to figure out was getting the C files to compile into an DLL that actually could be read without causing an UnsatisfiedLinkError.

After a lot of searching and trying things out, I was able to get the C source to compile to a DLL that could be called from Java. Since I am using MinGW, I found a page form mingw.org which described exactly how to build a DLL for JNI.

Sources:

查看更多
我只想做你的唯一
5楼-- · 2019-01-15 09:01

following dlamblin's revelation ;-) here's a python code. note that there are 2 different commands in most programming languages:

  • system
  • exec

system will issue a system command, exec indeed spawns a new process. thus:

C:\>python
>>> import os
>>> os.system("title berry tsakala")

which works inside a running program. Just find the java equivalent.

查看更多
登录 后发表回答