Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:08:13+00:00 2026-06-06T21:08:13+00:00

I am currently working on a program that has an embedded text editor. The

  • 0

I am currently working on a program that has an embedded text editor. The users are supposed to type java code in the editor. The code typed into the editor is then made into a string. I just want something that would check for missing parenthesis or a try without a catch, etc. It doesn’t need to be compiled. I’ve looked around quite a bit, but I’m still new to programming and can’t implement some of the harder stuff.

So to make it shorter: I’m looking for some java package that will analyze code for syntax errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:08:15+00:00Added an answer on June 6, 2026 at 9:08 pm

    As of Java 6 you can use JavaCompiler to compile the text and get back Diagnostic 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.

    Example code:

    File to Check

    public class HelloBuggyWorld {
        String test // missing a semicolon
    
        public static void main (String [] args) {
            System.out.println('Hello World!');  // should be double quoted
        }
    }
    

    Checker

    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(check("/path/to/HelloBuggyWorld.java"));
        }
    
        public static List<String> check(String file) {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
            Iterable<? extends JavaFileObject> compilationUnits =
                    fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file));
    
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();
    
            List<String> messages = new ArrayList<String>();
            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;
        }
    }
    

    Output

    From running the main method.

    [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
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am working with a embedded system that has the Linux OS. I
Here is my code currently. I'm making a java program that seaches Active Directory
I'm currently working on a program that has many of those the user SHOULD
I'm currently working with Java to write a program that does an EAI between
I am currently working on a program that utilizes RSSI to determine location based
I'm currently working on a C# program that creates a List, of object Task,
I have a python program that I am currently working on which is working
Currently I am working on a program that lets me handle data in form
I'm working on a program that will sort files based on extension I currently
I'm currently working on a Program, that presses buttons for me. I'm working on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.