4

JSLint4Java is a Java wrapper for JSLint. I need something like this for use in my GWT project, but the only way to use JSLint4Java seems to be from command line or with an ANT task. Does anyone know if there is any way to just import the JARs and use them in a project? When I try adding them to the GWT WAR folder, I get lots of errors like 'xxx cannot be resolved to a type'. Thanks.

Steven Morad
  • 2,511
  • 3
  • 19
  • 25

1 Answers1

5

Yes you can import the jar and use it like this :

import com.googlecode.jslint4java.Issue;
import com.googlecode.jslint4java.JSLint;
import com.googlecode.jslint4java.JSLintBuilder;
import com.googlecode.jslint4java.JSLintResult;


public static void lint(final String filePath) throws FileNotFoundException, IOException {
    String fileContent = Streams.read(new FileInputStream(new File(filePath))));
    JSLintBuilder builder = new JSLintBuilder();
    JSLint jsLint = builder.fromDefault();

    JSLintResult result = jsLint.lint("test.js", fileContent);
    for (Issue issue : result.getIssues()) {
        System.out.println(issue.toString());
    }

}
Fabien
  • 965
  • 3
  • 11
  • 23
  • Thanks — that's exactly correct! In case of trouble, there is [Javadoc](http://docs.jslint4java.googlecode.com/git/2.0.2/apidocs/index.html) available – Dominic Mitchell Aug 01 '12 at 16:58
  • One word of warning about the above code — it's using the system default encoding. You will likely want to explicitly choose UTF-8 instead. – Dominic Mitchell Aug 01 '12 at 17:01