But Instead of calling unusedcode.xml as a defined rule in PMD, I want to use my own rules which have been defined as a java file. Anybody has an idea how can I do that? Or Is there any other simpler way to achieve this?
Thanks,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
unusedcode.xml is actually a ruleset, which defines a couple of rules.
What you need to do is, to create your own ruleset, which references your custom java rule(s). With the ruleset you can make use of properties etc.
See https://pmd.github.io/pmd-5.8.1/customizing/howtowritearule.html and here especially the section "Put the WhileLoopsMustUseBracesRule rule in a ruleset file". If you put this ruleset xml file as a resource in the classpath (e.g. if you build your project with maven, you would put the ruleset under src/main/resources/customruleset.xml) you can reference it via your rulesets String: rulesets = "/customruleset.xml". PMD will search the classpath. You can also put it outside of the project and reference the file via an absolute path name.
Cheers,
Andreas
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Andreas, thanks for your reply. I have created my own rules in java class files. Those are not xml files, they are .java files. I wanna know how can I use them to apply on PMD?
Thanks,
Azadeh
Last edit: Azadeh 2017-09-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You'll need to compile your java files first using javac. Once this is done, you'll need to make sure, both PMD and your own code is on the classpath. You'll still need to write the ruleset XML file - otherwise you can't select the rule PMD should execute.
Which build tool do you use (Maven, ant, gradle, shell script, ...)?
Which operating system (Linux, Windows, Mac)? (the commands may differ...)
Do you use an IDE (Eclipse, IntelliJ, ...)?
How do you want to run PMD in the end? (Commandline, maven, ant, ...)
Thanks Andreas. I have compiled my java files and make the class files. I have created a ruleset XML file as well. I'm usimg windows, Eclipse, and Commandline.
What I've done are:
1- create a java class and the xml file excat the same as the sample..
2- compile the created class using javac.
3- create the jar file
4- put it in the PMD\lib.
8- run the PMD:
pmd.bat -d Source path -f xml -R Xml path
but I got this error.
Exception in thread "main" java.lang.NoClassDefFoundError: WhileLoopsMustUseBracesRule (wrong name: LPDetection/WhileLoopsMustUseBracesRule)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:333)
at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:261)
at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:216)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:161)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:126)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:111)
at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:19)
at net.sourceforge.pmd.PMD.doPMD(PMD.java:161)
at net.sourceforge.pmd.PMD.run(PMD.java:364)
at net.sourceforge.pmd.cli.PMDCommandLineInterface.run(PMDCommandLineInterface.java:158)
at net.sourceforge.pmd.PMD.main(PMD.java:344)
Anybody know how can I solve it?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, I assume, your rule is called LPDetection?
Then you'll just need to adjust the class attribute in the ruleset xml to actually reference your rule:
E.g. use this xml:
<?xml version="1.0"?><rulesetname="My custom rules"xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"><rulename="YourRuleName"message="Your violation message that should appear in the report"class="LPDetection"><description>
Describe your rule
</description><priority>3</priority><example><![CDATA[ public void doSomething() { while (true) x++; } ]]></example></rule></ruleset>
and this error
C:\Ph.D\IdentifierName\pmd-src-5.0.5\bin>pmd -d C:\Ph.D\IdentifierName\Workspace-IdentifierName\LADP\src -f xml -R C:\Ph.D\IdentifierName\Workspace-IdentifierName\LADP\src\mycustomrules.xml
java.lang.ClassNotFoundException: WhileLoopsMustUseBracesRule
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:333)
at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:261)
at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:216)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:161)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:126)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:111)
at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:19)
at net.sourceforge.pmd.PMD.doPMD(PMD.java:161)
at net.sourceforge.pmd.PMD.run(PMD.java:364)
at net.sourceforge.pmd.cli.PMDCommandLineInterface.run(PMDCommandLineInterface.java:158)
at net.sourceforge.pmd.PMD.main(PMD.java:344)
Couldn't find the class WhileLoopsMustUseBracesRule
Press any key to continue . . .
I don't know how can I manage to solve it :(
Azadeh
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It looks like, you are still using "WhileLoopsMustUseBracesRule" in the file mycustomrules.xml - try search for WhileLoopsMustUseBracesRule in this file - if it is found, replace it with "LPDetection".
By the way, you are using a very old PMD version (5.0.5). You can get the latest version (5.8.1) from https://pmd.github.io/#downloads
Hi,
I used this part of code to run PMD.
tring rulesets = "java-unusedcode" ;
String output_format = "text";
But Instead of calling unusedcode.xml as a defined rule in PMD, I want to use my own rules which have been defined as a java file. Anybody has an idea how can I do that? Or Is there any other simpler way to achieve this?
Thanks,
Hi,
unusedcode.xml is actually a ruleset, which defines a couple of rules.
What you need to do is, to create your own ruleset, which references your custom java rule(s). With the ruleset you can make use of properties etc.
See https://pmd.github.io/pmd-5.8.1/customizing/howtowritearule.html and here especially the section "Put the WhileLoopsMustUseBracesRule rule in a ruleset file". If you put this ruleset xml file as a resource in the classpath (e.g. if you build your project with maven, you would put the ruleset under
src/main/resources/customruleset.xml
) you can reference it via your rulesets String:rulesets = "/customruleset.xml"
. PMD will search the classpath. You can also put it outside of the project and reference the file via an absolute path name.Cheers,
Andreas
Hi Andreas, thanks for your reply. I have created my own rules in java class files. Those are not xml files, they are .java files. I wanna know how can I use them to apply on PMD?
Thanks,
Azadeh
Last edit: Azadeh 2017-09-29
You'll need to compile your java files first using
javac
. Once this is done, you'll need to make sure, both PMD and your own code is on the classpath. You'll still need to write the ruleset XML file - otherwise you can't select the rule PMD should execute.Which build tool do you use (Maven, ant, gradle, shell script, ...)?
Which operating system (Linux, Windows, Mac)? (the commands may differ...)
Do you use an IDE (Eclipse, IntelliJ, ...)?
How do you want to run PMD in the end? (Commandline, maven, ant, ...)
I think, we don't have a complete How-to-guide to write a custom rule... Maybe this other thread helps a little bit: https://sourceforge.net/p/pmd/discussion/188192/thread/f1ac6817/
Thanks Andreas. I have compiled my java files and make the class files. I have created a ruleset XML file as well. I'm usimg windows, Eclipse, and Commandline.
I tried to implement the sample existing in http://pmd.sourceforge.net/pmd-4.3.0/howtowritearule.html, but I couldn't run the PMD.
What I've done are:
1- create a java class and the xml file excat the same as the sample..
2- compile the created class using javac.
3- create the jar file
4- put it in the PMD\lib.
8- run the PMD:
pmd.bat -d Source path -f xml -R Xml path
but I got this error.
Exception in thread "main" java.lang.NoClassDefFoundError: WhileLoopsMustUseBracesRule (wrong name: LPDetection/WhileLoopsMustUseBracesRule)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:333)
at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:261)
at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:216)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:161)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:126)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:111)
at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:19)
at net.sourceforge.pmd.PMD.doPMD(PMD.java:161)
at net.sourceforge.pmd.PMD.run(PMD.java:364)
at net.sourceforge.pmd.cli.PMDCommandLineInterface.run(PMDCommandLineInterface.java:158)
at net.sourceforge.pmd.PMD.main(PMD.java:344)
Ok, I assume, your rule is called
LPDetection
?Then you'll just need to adjust the class attribute in the ruleset xml to actually reference your rule:
E.g. use this xml:
Please note, there is a newer version of the doc available at https://pmd.github.io/pmd-5.8.1/customizing/howtowritearule.html
Well, I got a new error:
Was passed main parameter 'C:\Ph.D\IdentifierName\Workspace-IdentifierName\LADP\src' but no main parameter was defined null!
It means that I did something wrong?
Azadeh
and this error
C:\Ph.D\IdentifierName\pmd-src-5.0.5\bin>pmd -d C:\Ph.D\IdentifierName\Workspace-IdentifierName\LADP\src -f xml -R C:\Ph.D\IdentifierName\Workspace-IdentifierName\LADP\src\mycustomrules.xml
java.lang.ClassNotFoundException: WhileLoopsMustUseBracesRule
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:333)
at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:261)
at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:216)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:161)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:126)
at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:111)
at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:19)
at net.sourceforge.pmd.PMD.doPMD(PMD.java:161)
at net.sourceforge.pmd.PMD.run(PMD.java:364)
at net.sourceforge.pmd.cli.PMDCommandLineInterface.run(PMDCommandLineInterface.java:158)
at net.sourceforge.pmd.PMD.main(PMD.java:344)
Couldn't find the class WhileLoopsMustUseBracesRule
Press any key to continue . . .
I don't know how can I manage to solve it :(
Azadeh
It looks like, you are still using "WhileLoopsMustUseBracesRule" in the file mycustomrules.xml - try search for WhileLoopsMustUseBracesRule in this file - if it is found, replace it with "LPDetection".
By the way, you are using a very old PMD version (5.0.5). You can get the latest version (5.8.1) from https://pmd.github.io/#downloads
For a reference for the command line options, see https://pmd.github.io/pmd-5.8.1/usage/running.html
Last edit: Azadeh 2017-10-04