0

currently I'm facing an issue where the URL that are outputted in the console is different from the actual URL display in the nav bar. I look at the value that I had regex, it was correct but when put into IE, it is different. The URL in the nav bar would be only a=test instead of a=test&c=import&f=&uid=user1 which is what is shown in the console. May I know how to solve this? Thanks.

Here is the codes for it.

    UserID user = new UserID();
    
    
    public static void main(String[] args) {
        
        String newUrl = replaceUserID(url);
        System.out.print("cmd.exe /c start iexplore -k " + newUrl);
        
        try{
            Process p = Runtime.getRuntime().exec("cmd.exe /c start iexplore " + newUrl);
            
            try{
                p.waitFor();
            }
            catch( InterruptedException ie ){
                System.out.println("InterruptedException " + ie.getMessage());
            }
            InputStream err = p.getErrorStream();
            int ctr = 0;
            if ( (ctr = err.available()) > 0 ){
                byte[] buf = new byte[ctr];
                System.out.println("Process failed with error:\n" + new String(buf, 0, ctr));
                }
            }
            catch(IOException ioe)
            {
                System.out.println("InterruptedException " + ioe.getMessage());
            }
    }
    
    public static String checkUserID(){     
        String username = System.getenv("USERNAME"); //grab user login id for windows
        return username;
    }
    
    public static String replaceUserID(String oldUrl) {
        String params = "http://example.com?a=test&c=import&f=&uid=userid";
        String username = checkUserID();
        
        try {
            Pattern p = Pattern.compile("uid=([^&%]+)");
            Matcher m = p.matcher(params);
            while (m.find()) {
//              System.out.println(m.group(1).toString());
                if(m.group(1).toString() != username) {
                    //replace url paremeters with correct userID.
                    String newUrl = params.replace(m.group(1).toString(), username);
                    return newUrl;
                }
                else {
                    System.out.println("The username is the same");
                    return params;
                }
            } 
        } catch (PatternSyntaxException ex) {
            // error handling
            ex.printStackTrace();
        }
        return oldUrl;
    }
user16320675
  • 135
  • 1
  • 3
  • 9
Anon1234
  • 23
  • 5
  • it works thanks @Thomas, u should put it as an answer so mark it as one – Anon1234 Nov 03 '21 at 07:08
  • Sure thing, done already. – Thomas Nov 03 '21 at 07:11
  • 2
    Besides your current issue you should become aware that calling `waitFor()` before reading a process’s output can lead to deadlocks. – Holger Nov 03 '21 at 07:54
  • how should i optimize my code to make it better for that part in that case, @Holger. – Anon1234 Nov 03 '21 at 07:56
  • 2
    In your example code, you are not really interested in the output but just read it to pass it to your own output channel. This can be done much better (simpler and more efficient) by not using a pipe at all: `Process p = new ProcessBuilder("cmd.exe", "/c", "start iexplore " + newUrl).inheritIO().start(); p.waitFor();` The [`inheritIO()`](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html#inheritIO--) does the trick. Otherwise, you have to read the process’s output up to its end before calling `waitFor()` (stdout and stderr at the same time, which can be tricky). – Holger Nov 03 '21 at 08:05
  • Thanks @Holger, appreciated the suggestion. It was informative. – Anon1234 Nov 03 '21 at 08:28

1 Answers1

0

& has a special meaning to the command line (concat commands, see here: How do I run two commands in one line in Windows CMD?).

You need to escape the url or maybe the entire command. If I remember correctly you need to put double quotes around it: "...iexplore -k \"" + newUrl + "\""

Thomas
  • 87,414
  • 12
  • 119
  • 157