How to Enable the display?

How to do setting:

  1. Enable the display of   < and > (that you use in your Java source codes) as   < and > respectively, not as a white space ‘ ‘, < and >;

  2. Color String literals in String markers “…” with gray and ensure double slashes “//” in any String literal to have the right color gray, not green;

  3. Color comment blocks in between /* … */ with green.

     import java.io.*; 
    
     class java2colorHTML { 
    
     public static String [] modifiers = {"class", "public", "private", "static", "void", "int", "long", }; 
     public static String [] other_keywords  = {"import", "throws", "new", "null", "true", "false", "for", "while", "do", "if", "else", "break", "return", "continue",}; // still need to collect many many more
    
     public static void main(String [] args) throws IOException { 
     // running: java java2colorHTML <fn.java>
    
     String txt = myIO.readFile(args[0]); 
    
         txt = txt.replace("<", "&lt;");
         txt = txt.replace(">", "&gt;"); // need to be done before "<br>" gets in!        
         txt = txt.replace("rn|n", "<br>"); // line break        
         txt = txt.replace(" ", "&nbsp;");
         txt = txt.replace("t", "&nbsp;&nbsp;&nbsp;&nbsp;"); // unicode 'u0009' for tab
    
         for (String w : modifiers) { 
             txt = txt.replaceAll(w + "&nbsp;", "<font color="#aa33ff">" + w + "</font>&nbsp;");
             // txt = txt.replaceAll(" " +w, "<font color="#9933ee"> " + w + "</font>");
         }
         for (String w : other_keywords) {
             txt = txt.replaceAll(w + "&nbsp;", "<font color="#3333ff">" + w + "</font>&nbsp;");
             txt = txt.replaceAll("&nbsp;" + w, "&nbsp;<font color="#3333ff">" + w + "</font>");
             txt = txt.replaceAll("\(" + w + "\)", "(<font color="#3333ff">" + w + "</font>)");
         }
    
         int bgn = 0;
         while (true) { // colour comment lines, if any
             bgn = txt.indexOf(""", bgn == 0 ? 0 : bgn+1); // get next bgn
             if (bgn == -1) break; // if no more literal strings
             int end = txt.indexOf(""", bgn+1); //find line break after bgn
    
             String str_Literals = txt.substring(bgn+1, end); // get current literal strings quote to the end
             str_Literals = str_Literals.replaceAll("</*[^<>]+>", ""); // remove embedded font tags, if any
             str_Literals = "<font color= "#a3a3a3">" + str_Literals + "</font>"; // coloring the literals
             txt = txt.substring(0, bgn) + str_Literals + 
                   txt.substring(end);
             bgn += str_Literals.length(); // set bgn to the end of current literals
                                           // avoid a bug here, because txt has been changed!
    
         }
    
    
         bgn = 0;
         while (true) { // color comment lines, if any
             bgn = txt.indexOf("//", bgn == 0 ? 0 : bgn+1); // get next bgn
             if (bgn == -1) break; // if no more comment
             int end = txt.indexOf("<br>", bgn); // find line break after bgn
             String comment = txt.substring(bgn, end); // get current comment
             comment = comment.replaceAll("</*[^<>]+>", ""); // remove embedded font tags, if any
             comment = "<font color="#009900">" + comment + "</font>"; // coloring a comment
             txt = txt.substring(0, bgn) + comment +
                   txt.substring(end);
             bgn += comment.length(); // set bgn to the end of current comment
                                      // avoid a bug here, because txt has been changed!
         }
    
         System.out.println("<html>rn<body>rn<tt>rn");
         System.out.println(txt);
         System.out.println("rn</tt>rn</body>rn</html>");
    
     }
    } 
    

Answer

I assume the question is in the title, how to display your nice HTML.

Instead of

 System.out.println("<html>rn<body>rn<tt>rn");
 System.out.println(txt);
 System.out.println("rn</tt>rn</body>rn</html>");

Make txt the complete HTML content.

 txt = "<!DOCTYPE html><html><head><meta charset="UTF-8"/></head>rn"
     + "<body>rn<tt>rn" 
     + txt + "rn</tt>rn</body>rn</html>";

I added the charset UTF-8.

Then save the HTML:

Path htmlPath = Paths.get(args[0] + ".html"); // Or a temporary file:
Path htmlPath = Files.createTempFile("nice-", ".html");
Files.writeString(htmlPath, txt);

Now let the operating system open (display) it:

Desktop.getDesktop().browse(htmlPath.toUri());