Menu

Cannot compile example java rule file

Help
2015-07-09
2018-02-08
  • Jimmy Philps

    Jimmy Philps - 2015-07-09

    I am trying to follow the instructions for making a custom rule via Java (not XPath). The end result is as follows:

    import net.sourceforge.pmd.lang.java.rule.*;
    import net.sourceforge.pmd.lang.java.ast.*;
    public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
    public Object visit(ASTWhileStatement node, Object data) {
    System.out.println("hello world");
    return data;
    }
    }

    So when I try to compile using the following command:

    javac -cp lib/pmd-java-5.3.2.jar WhileLoopsMustUseBracesRule.java

    The error message I receive is as follows:

    WhileLoopsMustUseBracesRule.java:3: error: cannot access ImmutableLanguage
    public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
    ^
    class file for net.sourceforge.pmd.lang.rule.ImmutableLanguage not found
    1 error

    NOTE: That arrow is actually aiming at the "class" part not "public"

    So I am trying to figure out how to compile this without errors so that my custom ruleset will recognize this as a valid class. (currently it says "Couldn't find the class WhileLoopsMustUseBracesRule").

    Any help is appreciated, thanks!

     

    Last edit: Jimmy Philps 2015-07-09
  • Andreas Dangel

    Andreas Dangel - 2015-07-09

    You'll need to add all jar files to the classpath while compiling. You can do this simply by this command:

    javac -cp lib/* WhileLoopsMustUseBracesRule.java

    Update: See below

    Then it should work :)

     

    Last edit: Andreas Dangel 2018-02-08
    • Andreas Dangel

      Andreas Dangel - 2018-02-08

      Correction: the asterisk (*) must be in quotes, otherwise your shell will expand it, so the correct command is:

      javac -cp 'lib/*' WhileLoopsMustUseBracesRule.java

       
  • Jimmy Philps

    Jimmy Philps - 2015-07-09

    Awesome, thanks for the response!

    One last issue is that when I run that javac -cp lib/* WhileLoopsMustUseBracesRule.java I get this error message:

    javac: invalid flag: lib/asm-5.0.3.jar
    Usage: javac <options>
    use -help for a list of possible options</options>

    asm-5.0.3.jar is the second item in that lib subdirectory, so I think that it is reading in the subsequent filenames in lib as additional parameters for some reason...

    So I guess what I am asking is, is lib/* the proper way to include multiple <options> files? </options>

    Also I realize this is more of a java compiling question so I apologize if it is wasting your time.

     
  • Jimmy Philps

    Jimmy Philps - 2015-07-09

    Actually, I have resolved that issue. I just needed to escape the asterisk. So I used the following instead:

    javac -cp lib/\* WhileLoopsMustUseBracesRule.java

    But thanks for all your help!

     

    Last edit: Jimmy Philps 2015-07-09
    • Azadeh

      Azadeh - 2017-10-02

      Hi, I did this steps, but when I run this script I got a new error:

      C:\Ph.D\IdentifierName\Workspace\Azadeh\src\WhileLoopsMustUseBracesRule.java:1: error: package net.sourceforge.pmd.lang.java.rule does not exist
      import net.sourceforge.pmd.lang.java.rule.;
      ^
      C:\Ph.D\IdentifierName\Workspace\Azadeh\src\WhileLoopsMustUseBracesRule.java:2: error: package net.sourceforge.pmd.lang.java.ast does not exist
      import net.sourceforge.pmd.lang.java.ast.
      ;
      ^
      C:\Ph.D\IdentifierName\Workspace\Azadeh\src\WhileLoopsMustUseBracesRule.java:4: error: cannot find symbol
      public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
      ^
      symbol: class AbstractJavaRule
      C:\Ph.D\IdentifierName\Workspace\Azadeh\src\WhileLoopsMustUseBracesRule.java:5: error: cannot find symbol
      public Object visit(ASTWhileStatement node, Object data) {
      ^
      symbol: class ASTWhileStatement
      location: class WhileLoopsMustUseBracesRule
      4 errors

       
  • Suman alungh

    Suman alungh - 2017-11-27

    @Azadeh, Were you able to get rid of these errors? I'm getting the same

     
  • Andreas Dangel

    Andreas Dangel - 2018-02-08

    Hi all,
    sorry for the late response.
    I've now created two sample projects, of how to build a custom rule:
    See https://github.com/pmd/pmd-examples
    Let me know, how we can improve such guides.
    Regards,
    Andreas

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.