0

Description : Actually I am looking the java code which is basically running in the background, But whenever I want to open a new notepad or excel file , It will capture those file as an input and display the result in output console.

How I can do that , any one can help me on this.

1 Answers1

1

The following mathod is based on Windows...

First of all, when opening files in software like Notepad and Excel, it is executed with a command line with parameters, that is, if Notepad is opened in E:\test.txt, the command line parameters for startup are

notepad E:\test.txt

In Windows, we can use the wmic command to get the startup parameters of an application. The specific usage is:

wmic process where caption="{exe_name}" get caption,commandline /value

For example, the cmd command to query the command line parameters opened in Notepad is:

wmic process where caption="notepad.exe" get caption,commandline /value

The returned result is similar to the following:

Caption=notepad.exe
CommandLine="C:\Windows\system32\NOTEPAD.EXE" H:\2019Summer\软件工程\任务.txt

The above "H:\2019Summer\软件工程\任务.txt" is the file I currently open by notepad. What we need to do is to parse the result String, here is my example java code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class GetOpenedFile {
    //QUERY_COMMAND
    private static String QUERY_COMMAND = "wmic process where caption=\"{0}\" get caption,commandline /value";
    private static String NOTEPAD_NAME = "notepad.exe";
    private static String EXCEL_NAME = "excel.exe";

    /**
     * get execName command line
     *
     * @param execName like notepad.exe, excel.exe
     * @return the all command line of the process {execName}
     */
    private static List<String> getExecCommandLines(String execName) {
        Runtime runtime = Runtime.getRuntime();
        List<String> commandLines = new ArrayList<>();
        try {
            Process process = runtime.exec(MessageFormat.format(QUERY_COMMAND, execName));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GB2312"));//set your cmd charset(default value is utf8)
            String caption = null;
            while (true) {
                String s = bufferedReader.readLine();
                if (s == null) {
                    break;
                }
                if (s.length() == 0) {//skip blank string
                    continue;
                }
                //get the file name
                if (s.startsWith("Caption")) {
                    caption = s.substring("Caption=".length());
                    continue;
                }
                if (s.startsWith("CommandLine=") && caption != null) {
                    int index = Math.max(s.indexOf(caption), s.indexOf(caption.toUpperCase()));//maybe the exe file name is ALL UPPER CASE, eg. NOTEPAD.EXE
                    index += caption.length()
                            + 1;//Double quotes "
                    String commandLine = s.substring(index);// command Line
                    commandLine = commandLine.stripLeading();//strip leading white space
                    commandLines.add(commandLine);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return commandLines;
    }

    /**
     * get notepad opened files
     *
     * @return notepad opened files
     */
    public static List<String> getNotepadOpenedFiles() {
        List<String> commandLines = getExecCommandLines(NOTEPAD_NAME);
        return commandLines.stream()
                .filter(s -> s.length() > 0)//skip empty command line
                .collect(Collectors.toList());
    }

    /**
     * get excel opened files
     * @return excel opened files
     */
    public static List<String> getExcelOpenedFiles() {
        List<String> commandLines = getExecCommandLines(EXCEL_NAME);
        return commandLines.stream()
                .filter(s -> s.length() > 0)//skip empty command line
                .map(s -> {             //map result of "filename" to filename
                    if (s.startsWith("\"") && s.endsWith("\"")) {
                        return s.substring(1, s.length() - 1);
                    } else {
                        return s;
                    }
                })
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        //console printed
//        [H:\2019Summer\软件工程\任务.txt]
//        [E:\info.xlsx]
        System.out.println(getNotepadOpenedFiles());
        System.out.println(getExcelOpenedFiles());
    }
}
  • Hi Actually your code will talk about the file which is already opened (Notepad / Excel) and it will capture those file and printing the result in console. I have already executed this code but i am getting the blank output as a result. Output look like ----------------------- [ ] [ ] I am looking the code which is basically running in the background, But whenever i want to open notepad or excel file , It will capture those file as an input and display the result in output console with successful message. – Raghuvir Prasad Mar 10 '21 at 05:31
  • Hi Guys, Repeating the same question again -- Actually I am looking the java code which is basically running in the background, But whenever i want to open notepad or excel file , It will capture those file as an input and display the result in output console with successful message. Please help me on this. – Raghuvir Prasad Mar 10 '21 at 12:15