Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of Java 6 you can use <a href="http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html">JavaCompiler</a> to compile the text and get back <a href="http://docs.oracle.com/javase/6/docs/api/index.html?javax/tools/Diagnostic.html">Diagnostic</a> objects that tell you what problems the file has (if any). So for your example you'd need to take the content of the editor and pass it to the JavaCompiler, run it, and report back any problems. Example that follows assumes editor text written out to a file. </p> <p>Example code:</p> <p><strong>File to Check</strong></p> <pre><code>public class HelloBuggyWorld { String test // missing a semicolon public static void main (String [] args) { System.out.println('Hello World!'); // should be double quoted } } </code></pre> <p><strong>Checker</strong></p> <pre><code>import java.util.ArrayList; import java.util.Arrays; import java.util.Formatter; import java.util.List; import java.util.Locale; import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; public class JavaSyntaxChecker { public static void main(String[] args) { System.out.println(JavaSyntaxChecker.check("/path/to/HelloBuggyWorld.java")); } public static List&lt;String&gt; check(String file) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable&lt;? extends JavaFileObject&gt; compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file)); DiagnosticCollector&lt;JavaFileObject&gt; diagnostics = new DiagnosticCollector&lt;JavaFileObject&gt;(); compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call(); List&lt;String&gt; messages = new ArrayList&lt;String&gt;(); Formatter formatter = new Formatter(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { messages.add(diagnostic.getKind() + ":\t Line [" + diagnostic.getLineNumber() + "] \t Position [" + diagnostic.getPosition() + "]\t" + diagnostic.getMessage(Locale.ROOT) + "\n"); } return messages; } } </code></pre> <p><strong>Output</strong></p> <p>From running the <code>main</code> method.</p> <pre><code>[ERROR: Line [5] Position [124] HelloBuggyWorld.java:5: unclosed character literal , ERROR: Line [5] Position [126] HelloBuggyWorld.java:5: ';' expected , ERROR: Line [5] Position [131] HelloBuggyWorld.java:5: not a statement , ERROR: Line [5] Position [136] HelloBuggyWorld.java:5: ';' expected , ERROR: Line [5] Position [137] HelloBuggyWorld.java:5: unclosed character literal ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload