Why can I add context menus for some extensions, but no one uses Reg and Java

I am trying to modify the Windows registry to allow a context menu for certain audio file extensions so that my application can be launched in the specified files. The application is written in Java, but wrapped in an exe wrapper using Winrun4j.

I used this example. How to create a context menu for my software? to try to do it. The depressing thing is this demo adds a context menu for .txt files, and it works, and if I crack the code and add it to my application for the .txt file, it also works. But if I just changed the code for working with audio files, for example .mp3, error messages are not reported, but it does not work, in the context menu for .mp3s

Checking the code to see if a particular group is associated with another application, and whether it affects the value entered in the registry, but both audio files and text files are associated with another application (Audio Vlc, txt, UltraEdit) so this is not a problem. I tried to ignore these applications and add directly to Software / .mp3 / classes instead of Software / Vlc / classes, but that didn't matter.

Here is the result when I run:

C:\Program Files (x86)\Jthink\SongKong>songkong.exe 10/02/2014 17.13.10:com.jthink.songkong.Windows.AddContextMenu:buildLaunchCommand:SEVERE: Architecture is:x86 10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp3 /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3 /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp3) 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp3\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp3\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP3:AddContextMenu:true 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.ogg /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.ogg) 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.ogg\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.ogg\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: OGG:AddContextMenu:true 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.flac /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.flac) 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.flac\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.flac\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32 /ve /t REG _SZ /d Fix with SongKong /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32\Command /v e /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: FLAC:AddContextMenu:true 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4a /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a /ve 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4a) 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4a\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4a\Shell\SongKong32 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4A:AddContextMenu:true 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp4 /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4 /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp4) 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp4\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp4\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP4:AddContextMenu:true 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4p /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4p) 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4p\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4p\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4P:AddContextMenu:true 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.wma /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.wma) 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.wma\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.wma\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32 /ve /t REG_ SZ /d Fix with SongKong /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: WMA:AddContextMenu:true 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4b /ve 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\(value not set) 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\.m4b\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\.m4b\Shell\SongKong32 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32 /ve /t REG_SZ /d Fix with SongKong /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32\Command /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4B:AddContextMenu:true 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe" -g -m "%1" 10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.txt /ve 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt /ve 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\Text Document 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32 /ve / t REG_SZ /d Fix with SongKong /f 10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32\Comma nd /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f 10/02/2014 17.13.13:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: txt:AddContextMenu:true 

Here is the relevant code

 public class AddContextMenu { public static void addContextMenu() { String launchCmd = buildLaunchCommand(); String INTERNAL_NAME = "SongKong"; final String osArch = System.getProperty("os.arch").toLowerCase(); if(!osArch.contains("64")) { INTERNAL_NAME = "SongKong"; } String DISPLAY_NAME = "Fix with SongKong"; for(AudioFormat audioFormat: AudioFormat.values()) { if(audioFormat.getFileSuffix().length()>0) { boolean success = ContextMenuManager.createContextMenu( INTERNAL_NAME, DISPLAY_NAME, SongKong.class, "." + audioFormat.getFileSuffix(), launchCmd); MainWindow.logger.severe(audioFormat+":AddContextMenu:"+success); } } //For testing boolean success = ContextMenuManager.createContextMenu( INTERNAL_NAME, DISPLAY_NAME, SongKong.class, ".txt", launchCmd); MainWindow.logger.severe("txt"+":AddContextMenu:"+success); } static protected String buildLaunchCommand() { File userDir = new File(System.getProperty("user.dir")); final String osArch = System.getProperty("os.arch").toLowerCase(); MainWindow.logger.severe("Architecture is:"+osArch); if(osArch.contains("64")) { return "\""+ userDir.getAbsolutePath() + "\\SongKong64.exe\" -g -m \"%1\""; } else { return "\""+ userDir.getAbsolutePath() + "\\SongKong.exe\" -g -m \"%1\""; } } } package com.jthink.songkong.Windows; import com.jthink.songkong.ui.MainWindow; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; final public class ContextMenuManager { static public boolean createContextMenu(String internalName, String displayName, Class mainClass, String targetExtention, String launchCmd) { MainWindow.logger.severe("launchCmd:"+launchCmd); String targetKeyNode = inferTargetKeyNode(targetExtention, internalName); MainWindow.logger.severe("targetKeyNode"+targetKeyNode); if (keyNodeExists(targetKeyNode)) { // TODO: Handle existence of Registry key-node with the same name MainWindow.logger.severe("targetKeyNodeExists"+targetKeyNode); return false; } String[][] registryCmds = buildRegistryAddCommands( targetKeyNode, displayName, launchCmd); boolean operationSuccessful = true; for (int i = 0; i < registryCmds.length; i++) { operationSuccessful &= executeCommand(registryCmds[i]); } if (!operationSuccessful) { removeContextMenu(internalName, targetExtention); // "Roll back" } return operationSuccessful; } static public boolean removeContextMenu(String internalName, String targetExtention) { /* * Implementation note: * This way we might be deleting another app context-menu entry if * it happens to have the same name as ours. It may be a good idea to * add an custom value to our key-node with a name amd or data highly * unlikely to have been inserted by another app as well. This way we * should minimize the risk of accidentally deleting another app * entry to next to zero. */ String targetKeyNode = inferTargetKeyNode(targetExtention, internalName); if (!keyNodeExists(targetKeyNode)) { return true; } String[][] registryCmds = buildRegistryDeleteCommands(targetKeyNode); boolean operationSuccessful = true; for (int i = 0; i < registryCmds.length; i++) { operationSuccessful &= executeCommand(registryCmds[i]); } if (!operationSuccessful) { // TODO: Do some "rolling forward" (?) // (Rather difficult, so we better ignore it and hope we didn't // break anything :D) } return operationSuccessful; } static protected String[][] buildRegistryAddCommands(String targetKeyNode, String displayName, String launchCmd) { String[][] cmds = new String[2][]; // REG ADD <target_key_node>\Shell\<internal_name> /ve /t REG_SZ /d <display_name> /f cmds[0] = new String[]{ "REG", "ADD", targetKeyNode, "/ve", "/t", "REG_SZ", "/d", displayName, "/f"}; // REG ADD <target_key_node>\Shell\<internal_name>\Command /ve /t REG_SZ /d <properly_escaped_launch_command> /f String escapedLaunchCmd = launchCmd.replaceAll("\"", "\\\\\""); cmds[1] = new String[]{ "REG", "ADD", targetKeyNode + "\\Command", "/ve", "/t", "REG_SZ", "/d", escapedLaunchCmd, "/f"}; return cmds; } static protected String[][] buildRegistryDeleteCommands( String targetKeyNode) { String[][] cmds = new String[1][]; /* REG ADD <target_key_node>\Shell\<internal_name> /f */ cmds[0] = new String[]{"REG", "DELETE", targetKeyNode, "/f"}; return cmds; } static protected boolean executeCommand(String[] cmd) { return executeCommand(cmd, null); } static protected boolean executeCommand(String[] cmd, List<String> output) { StringBuffer sb = new StringBuffer(); for(String c:cmd) { sb.append(c+" "); } MainWindow.logger.severe("Cmd:"+sb); try { ProcessBuilder pb = new ProcessBuilder(cmd); Process proc = pb.start(); /*BufferedReader brErr = new BufferedReader(new InputStreamReader( proc.getErrorStream())); String lineErr; while ((lineErr = brErr.readLine()) != null) { System.err.println(lineErr); }*/ if (output != null) { BufferedReader br = new BufferedReader(new InputStreamReader( proc.getInputStream())); String line; while ((line = br.readLine()) != null) { output.add(line); } } int exitStatus = proc.waitFor(); return (exitStatus == 0); } catch (Exception x) { x.printStackTrace(); } return false; } static protected String inferTargetKeyNode(String targetExtension, String internalName) { try { //Search //ie Pattern pattern = Pattern.compile( "\\s*\\(.+\\)\\s*REG_SZ\\s*(.*)"); String[] cmd = {"REG", "QUERY", "", "/ve"}; String classKeyNode = "HKCR\\" + targetExtension; boolean findAssociatedClass = true; while (findAssociatedClass) { cmd[2] = classKeyNode; List<String> output = new ArrayList<>(); executeCommand(cmd, output); findAssociatedClass = false; //Look to see whther we should associate with suffix directly or via another application (iemp4 or Vlc files) for (String line : output) { Matcher matcher = pattern.matcher(line); if (matcher.find() && keyNodeExists("HKCR\\" + matcher.group(1))) { classKeyNode = "HKCR\\" + matcher.group(1); findAssociatedClass = true; break; } } } //Place in User Space rather than root space (which need admin permissions classKeyNode = classKeyNode .replaceFirst("HKCR", "HKCU\\\\Software\\\\Classes"); return classKeyNode + "\\Shell\\" + internalName; } catch (Exception x) { x.printStackTrace(); } return ""; } static protected boolean keyNodeExists(String keyNode) { if ((keyNode == null) || (keyNode.trim().length() == 0)) { throw new RuntimeException( "Invalid or non-existent Registry-key node !"); } try { String[] cmd = {"REG", "QUERY", keyNode}; return executeCommand(cmd); } catch (Exception x) { x.printStackTrace(); } return false; } } 
0
source share

Source: https://habr.com/ru/post/1482943/


All Articles