Skip to content

Instantly share code, notes, and snippets.

@asdf913
Created July 1, 2023 01:21
Show Gist options
  • Save asdf913/0dc8fa930d248ca4ecfcb48d045c1c85 to your computer and use it in GitHub Desktop.
Save asdf913/0dc8fa930d248ca4ecfcb48d045c1c85 to your computer and use it in GitHub Desktop.
IsTriggeredByDoubleClickOrCommand.java - Detect a program is triggered by Double Click or Command - Microsoft Windows Only
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.Objects;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
public class IsTriggeredByDoubleClickOrCommand {
public static void main(final String[] args) {
//
System.out.println("isTriggerredByDoubleClick()=" + isTriggerredByDoubleClick());
//
System.out.println("isTriggerredByCommand() =" + isTriggerredByCommand());
//
}
private static Boolean isTriggerredByDoubleClick() {
//
final ProcessHandle ph = ProcessHandle.current();
//
if (ph != null) {
//
final FileSystem fs = FileSystems.getDefault();
//
if (Objects.equals(getName(getClass(fs)), "sun.nio.fs.WindowsFileSystem")) {
//
String executablePath = null;
//
try {
//
final Runtime runtime = Runtime.getRuntime();
//
final Process p = runtime != null
? runtime.exec(
String.format("wmic process where \"ProcessId=%1$s\" get ExecutablePath", ph.pid()))
: null;
//
try (final InputStream is = p != null ? p.getInputStream() : null) {
//
executablePath = StringUtils.trim(IOUtils.toString(is, "utf-8"));
//
} // try
//
} catch (final IOException e) {
//
e.printStackTrace();
//
} // try
//
return Boolean.valueOf(StringUtils.endsWithIgnoreCase(executablePath, "javaw.exe"));
//
} // if
//
} // if
//
return null;
//
}
private static Boolean isTriggerredByCommand() {
//
final ProcessHandle ph = ProcessHandle.current();
//
if (ph != null) {
//
final FileSystem fs = FileSystems.getDefault();
//
if (Objects.equals(getName(getClass(fs)), "sun.nio.fs.WindowsFileSystem")) {
//
String executablePath = null;
//
try {
//
final Runtime runtime = Runtime.getRuntime();
//
final Process p = runtime != null
? runtime.exec(
String.format("wmic process where \"ProcessId=%1$s\" get ExecutablePath", ph.pid()))
: null;
//
try (final InputStream is = p != null ? p.getInputStream() : null) {
//
executablePath = StringUtils.trim(IOUtils.toString(is, "utf-8"));
//
} // try
//
} catch (final IOException e) {
//
e.printStackTrace();
//
} // try
//
return Boolean.valueOf(StringUtils.endsWithIgnoreCase(executablePath, "java.exe"));
//
} // if
//
} // if
//
return null;
//
}
private static Class<?> getClass(final Object instance) {
return instance != null ? instance.getClass() : null;
}
private static String getName(final Class<?> instance) {
return instance != null ? instance.getName() : null;
}
}
@asdf913
Copy link
Author

asdf913 commented Jul 1, 2023

@asdf913
Copy link
Author

asdf913 commented Jul 1, 2023

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you do not want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment