如何从命令行Java应用程序更改命令提示符(控制台)窗口标题? [英] How to change command prompt (console) window title from command line Java app?

查看:392
本文介绍了如何从命令行Java应用程序更改命令提示符(控制台)窗口标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从java命令行应用程序更改和更新命令提示符窗口的标题?每次我运行我的应用程序,命令提示符窗口标题显示:
C:\WINDOWS\system32\cmd.exe - java MyApp p>

我想在java程序运行时更改和更新窗口标题,例如wget(win32)更新标题中的下载状态:解决方案

虽然我自己没有尝试过,在Windows中,可以使用Win32 API调用命令用于创建C标题


$ b

编写库将涉及将C源代码写入由 javah 生成的C头文件。



ChangeTitle.h 标题是以下内容:

  / *请勿编辑此文件 - 它是机器生成的* / 
#include< jni.h>
/ * ChangeTitle类的标题* /

#ifndef _Included_ChangeTitle
#define _Included_ChangeTitle
#ifdef __cplusplus
externC{
#endif
/ *
* Class:ChangeTitle
*方法:setTitle
*签名:(Ljava / lang / String;)V
* /
JNIEXPORT void JNICALL Java_ChangeTitle_setTitle
(JNIEnv *,jclass,jstring);

#ifdef __cplusplus
}
#endif
#endif

现在,实现 ChangeTitle.c

  #include< windows.h> 
#include< stdio.h>
#include< conio.h>
#include< jni.h>
#includeChangeTitle.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 函数将更改为UTF-8编码的C字符串,然后发送到 SetConsoleTitle 函数,根据函数名称建议,更改控制台的标题。



(注意:只是将字符串传递到 SetConsoleTitle 函数中可能会有一些问题,但是根据文档,它也接受Unicode '



上面的代码基本上是从第3.2节:访问字符串 Java Native Interface程序员指南和规范,以及 SetConsoleTitle



有关错误检查的更多涉及示例代码,请参阅第3.2节:访问字符串 SetConsoleTitle 功能页。



构建DLL



最终花费了大量时间的部分是让C语言编译成一个DLL实际上可以在不导致 UnsatisfiedLinkError



经过大量的搜索和尝试,我能够获得C源代码到可以从Java调用的DLL。因为我使用MinGW,我找到一个页面形式 mingw.org 其中描述了如何为JNI创建DLL



资源:




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%].

解决方案

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:

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:

这篇关于如何从命令行Java应用程序更改命令提示符(控制台)窗口标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆