Menu

Adding custom rule to PMD - Class not found issue

2015-04-06
2017-11-27
  • Adarsh Kashyap

    Adarsh Kashyap - 2015-04-06

    I am trying to execute this example shown here
    http://pmd.sourceforge.net/pmd-5.3.0/customizing/howtowritearule.html

    WhileLoopsMustUseBracesRule class not found is shown [one of the standard issue for beginners i believe]

    Method tried :

    1) cd pmd-5.3.0
    2) created WhileLoopsMustUseBracesRule.java and ruleset.xml as per shown in the page.
    3) javac -cp [all the required jar files seperated by ; since it is windows pc ] WhileLoopsMustUseBracesRule.java
    4) bin/pmd.bat -d [code on which pmd has to check the rule] -f xml -R ruleset.xml

    the above steps are giving me no class found error,here developer has commented saying these steps work [ http://sourceforge.net/p/pmd/discussion/188192/thread/7a34d224/ ]
    Please let me know if i am missing anything.

    I tired adding class file directly to classpath as well

    Other methods tried :

    1) compiled the WhileLoopsMustUseBracesRule.java file and got the .class file.
    2) created a jar using jar -cf jar_name jar_file command
    3) added that jar file in the class path , using set CLASSPATH and also tried adding directly in the batch file
    i.e. java -classpath %TOPDIRECTORY%\lib*;newrule.jar %OPTS% %CLASSNAME%
    something like this.

    please help !

     
    • Suman alungh

      Suman alungh - 2017-11-27

      Hi Adarsh,

      I am a beginner and trying to follow what you did but I am having some problem. How did you manage to get the .class file. I can't compile my .java file. I created the exact WhileLoopsMustUseBracesRule.java file and the exact ruleset.xml and when I try to javac WhileLoopsMustUseBracesRule.java then it gives me below error. Can you please help.

      javac WhileLoopsMustUseBracesRule.java
      WhileLoopsMustUseBracesRule.java:1: error: package net.sourceforge.pmd.lang.ast
      does not exist
      import net.sourceforge.pmd.lang.ast.;
      ^
      WhileLoopsMustUseBracesRule.java:2: error: package net.sourceforge.pmd.lang.jav
      .ast does not exist
      import net.sourceforge.pmd.lang.java.ast.
      ;
      ^
      WhileLoopsMustUseBracesRule.java:3: error: package net.sourceforge.pmd.lang.jav
      .rule does not exist
      import net.sourceforge.pmd.lang.java.rule.*;
      ^
      WhileLoopsMustUseBracesRule.java:5: error: cannot find symbol
      public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
      ^
      symbol: class AbstractJavaRule
      WhileLoopsMustUseBracesRule.java:6: error: cannot find symbol
      public Object visit(ASTWhileStatement node, Object data) {
      ^
      symbol: class ASTWhileStatement
      location: class WhileLoopsMustUseBracesRule
      WhileLoopsMustUseBracesRule.java:13: error: cannot find symbol
      private boolean hasBlockAsFirstChild(Node node) {
      ^
      symbol: class Node
      location: class WhileLoopsMustUseBracesRule
      WhileLoopsMustUseBracesRule.java:7: error: cannot find symbol
      Node firstStmt = node.jjtGetChild(1);
      ^
      symbol: class Node
      location: class WhileLoopsMustUseBracesRule
      WhileLoopsMustUseBracesRule.java:11: error: cannot find symbol
      return super.visit(node,data);
      ^
      symbol: variable super
      location: class WhileLoopsMustUseBracesRule
      WhileLoopsMustUseBracesRule.java:14: error: cannot find symbol
      return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceo
      ASTBlock));

      ^
      symbol: class ASTBlock
      location: class WhileLoopsMustUseBracesRule
      9 errors

       
  • Adarsh Kashyap

    Adarsh Kashyap - 2015-04-07

    I found the solution!

    Had to set CLASSPATH separately using

    set CLASSPATH=path\to\my\classfile
    and then in the batch i had to add %CLASSPATH% , otherwise using -cp was resetting the classpath i guess. This worked for me.

    Thanks

     
  • Nikhil Pawar

    Nikhil Pawar - 2015-04-27

    Hey could you please tell me what exactly did you change in pmd.bat to set the classpath?

     
  • Andreas Dangel

    Andreas Dangel - 2015-04-27

    You have the following options:

    1. copy the jar file, which contains your custom rule, into the lib directory of the pmd-installation. That's all you need to do, all jar files will be picked up in the lib directory automatically.

    2. Modify the file pmd.bat to have the following content. This directly adds C:\Path\To\Your\Custom\Rule to the classpath. This Path needs to contain the compiled class files for your custom rule.

      @echo off
      set TOPDIR=%~dp0..
      set OPTS=
      set MAIN_CLASS=net.sourceforge.pmd.PMD
      
      java -classpath %TOPDIR%\lib\*;C:\Path\To\Your\Custom\Rule %OPTS% %MAIN_CLASS% %*
      
    3. Modify the file pmd.bat to set the environment variable CLASSPATH and use this variable later on:

      @echo off
      set TOPDIR=%~dp0..
      set OPTS=
      set MAIN_CLASS=net.sourceforge.pmd.PMD
      set CLASSPATH=C:\Path\To\Your\Custom\Rule
      
      java -classpath %TOPDIR%\lib\*;%CLASSPATH% %OPTS% %MAIN_CLASS% %*
      

      You could remove the set CLASSPATH=... line and set this environment variable outside of the pmd.bat file. This would have the advantage, that you don't need to adjust the pmd.bat file, if you change the classpath, but just change the environment variable.

    Hope that helps :)

     

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.