wood burning stoves 2.0*
The moose likes Java in General and the fly likes Intermediate/Advanced Java Users. Pl help with static profilers (PMD) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Intermediate/Advanced Java Users. Pl help with static profilers (PMD)" Watch "Intermediate/Advanced Java Users. Pl help with static profilers (PMD)" New topic
Author

Intermediate/Advanced Java Users. Pl help with static profilers (PMD)

amitinus
Greenhorn

Joined: Jul 01, 2001
Posts: 6
Hello,

I am an intermediate level programmer of Java, slightly on the beginners side. I have the followwing issue: I need help as I have run out of ideas on how to takle the problem.

I want to use a static profiler, one that parser through a .java file and reports any possible unused variables, any if statements having a blank body, any methods declared in the class, that are not called from main() or any other methods in the class, an empty catch body, duplicate import statements, redundant importing of java.lang.*; , creating a new String object instead of assigning a String variable to a literal (which is faster)etc, etc

These programming styles are not the most efficient and are described in, what are called - rulesets. One free static profiler is PMD. http://pmd.sourceforge.net
It has a downloadable file pmd-bin-1.8.zip <approx 4MB in size) which gets extracetd to a directory, say C:\pmd\pmd-1.8... and has 3 sub directories

dir <...\pmd-1.8\lib> has 5 jar files that provide the profiling utility.
-----------------------
jaxen-core-1.0-fcs.jar;
pmd-1.8.jar;
saxpath-1.0-fcs.jar;
xercesImpl-2.0.2.jar;
xmlParserAPIs-2.0.2.jar;
-----------------------
Add classpaths to point to those jar files.
Have suitable PATH set to the relevant Java compiler, either from Sun or IBM. I use 1.4.x

Now i create a java source file called TestHprof.java with some code and some redundant imports, duplicate imports, some variables that are left uninitialized and are never used, some if statements with nothing in their body. some methods in the class which are not called from main or any other method, etc and otehr such code, examples of which are available on the PMD webpage http://pmd.sourceforge.net

The .java file need not even be compiled. The PMD utility is run as follows:
Store your .java file in some dir say C:\foo
on the command prompt, cd ...\pmd-1.8\etc> This is where you have the run.bat file which you use as follows

SHELL_prompt>run C:\foo\TestHprof.java xml rulesets/basic.xml
It should ideally show results like XML tags on the same command prompt, saying some part of code is unused variables on line xxx, or some function is never called or has no body on line xxx.

After I use the run batch file or issue a "java -cp ... javafilename xml ruleset_name" command I get the following message !!!

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/parsers/DocumentBuilderFactory
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:100)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:88)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:59)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:78)
at net.sourceforge.pmd.PMD.main(PMD.java:108)
================================================================
It fails to report these programming practices. Can anyone one of the experienced Java programmers try this excercise and let me know where I may be going wrong, time permitting.
A sincere Thank you.

Best Regards,
-Amit
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
Add classpaths to point to those jar files.


Here's the problem, check your classpath.
amitinus
Greenhorn

Joined: Jul 01, 2001
Posts: 6
Hi Tobias,

Thank you for an early reply.
To reflect what I am working on, and how I set my path,classpath I am attaching the contents of my file "pather.bat". It does have the classpath set to the multiple .jar files. Please check through it. I have copied the Directory location from windows icon right click-properties and then the name. So I am doubly sure I have not misspelt any dir names or file names this time.

I then CD to the directory pmd-1.8\etc> and then issue a
"run F:\amit.t\java_profilers\PMD\pmd-1.8\etc\TestHprof.java xml/text rulesets/basic.xml" command.
It still gives me the same error message as below:
=============================================================
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/parsers/DocumentBuilderFactory
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:100)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:88)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:59)
at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:78)
at net.sourceforge.pmd.PMD.main(PMD.java:108)
============================================================

where I have placed the required java file in the pmd-1.8\etc folder (for convinience) I even tried moving the java file to F:\ (root) and issued a command "run F:\TestHprof.java xml rulesets/basic.xml" and I still get the same error message.

If you can, please go through the pather.bat file below, which illustrates my original path/classpath, how I set my path/classpath and other environment variables like PMD_HOME and the final path/classpath.

If I have set the correct classpaths, and path, and it still doesnt work,
could it have a problem with the xerces...jar files. They are a standard distribution from the website. I have downloaded the binary pmd-bin-1.8.zip file worth 4MB and it is for Windows platform. (I am sure it wouldn't have worked , if it was for a Unix platform)

Following are the contents of my "pather.bat" file

==========================================================================
REM Before running the pather.bat file the path and classpath is as set when the
REM system boots. I cannot set environment variables from
REM Start-settings-Control Panel-System-Advanced-Environment Variables on a
REM Windows 2000 Professional Operating System

REM Initial PATH is as follows: (after system boots, from the command prompt)
REM Path=F:\Perl\bin;C:\Program Files\Compaq\Compaq Management Agents\Dmi\Win32\Bin;
REM C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;c:\system;c:\j2sdk1.4.1_06\bin
REM PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

REM Initial CLASSPATH is as follows after system boots, from the command prompt)
REM CLASSPATH=.
REM =============================================================

REM adding a path to Java compiler

set JAVA_HOME=F:\Program Files\IBM\Websphere Studio\runtimes\base_v5\java\bin
set path=%JAVA_HOME%;%path%

REM define a PMD_HOME for the folder containing the pmd jar files
REM set PMD_HOME=F:\amit.t\java_profilers\PMD\pmd-1.8\lib
REM =============================================================
set PMD_HOME=F:\amit.t\java_profilers\PMD\pmd-1.8\lib


REM add classpath to the PMD jar files
REM =============================================================
set classpath=;%classpath%;

REM use the following to set classpath to PMD
REM =============================================================
set classpath=%PMD_HOME%\jaxen-core-1.0-fcs.jar;%classpath%
set classpath=%PMD_HOME%\pmd-1.8.jar;%classpath%
set classpath=%PMD_HOME%\saxpath-1.0-fcs.jar;%classpath%
set classpath=%PMD_HOME%\xercesImpl-2.0.2.jar;%classpath%
set classpath=%PMD_HOME%\xmlParserAPIs-2.0.2.jar;%classpath%


REM use the following to set classpath to Cougar Version of PMD
REM =============================================================
REM set COUHOME=F:\amit.t\java_profilers\cpmd\pmd\lib

REM set classpath=%COUHOME%\pmd-0.3.jar;%classpath%
REM set classpath=%COUHOME%\xercesImpl-2.0.2.jar;%classpath%
REM set classpath=%COUHOME%\xmlParserAPIs-2.0.2.jar;%classpath%

REM add an additional classpath to the PerfAnal.jar file
REM =============================================================
REM set classpath=F:\amit.t\java_profilers\PerfAnal.jar;%classpath%

REM =============================================================
REM AFTER executing this batch file as pather.bat the following classpath
REM and Environment Variables get set.

REM Path=F:\Program Files\IBM\Websphere Studio\runtimes\base_v5\java\bin;F:\Perl\bin;
REM C:\Program Files\Compaq\Compaq Management Agents\Dmi\Win32\Bin;
REM C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;c:\system;c:\j2sdk1.4.1_06\bin
REM PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH


REM CLASSPATH=F:\amit.t\java_profilers\PMD\pmd-1.8\lib\xmlParserAPIs-2.0.2.jar;
REM CLASSPATH=F:\amit.t\java_profilers\PMD\pmd-1.8\lib\xercesImpl-2.0.2.jar;
REM CLASSPATH=F:\amit.t\java_profilers\PMD\pmd-1.8\lib\saxpath-1.0-fcs.jar;
REM CLASSPATH=F:\amit.t\java_profilers\PMD\pmd-1.8\lib\pmd-1.8.jar;
REM CLASSPATH=F:\amit.t\java_profilers\PMD\pmd-1.8\lib\jaxen-core-1.0-fcs.jar;;.;

REM the other variable PMD_HOME=F:\amit.t\java_profilers\PMD\pmd-1.8\lib
============================================================================

I did try to provide exact data reflecting my environment on the OS that I use (Win 2000 Professional). I hope it helps you for some "troubleshooting" !!!

Sincerely,
- Amit
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
Delete this lines, and see if it works:

REM adding a path to Java compiler

set JAVA_HOME=F:\Program Files\IBM\Websphere Studio\runtimes\base_v5\java\bin
set path=%JAVA_HOME%;%path%


Wich java are you using? Looks like you have two installed, one with websphere and one with the j2sdk.

The problem is, that your profiler can't find the class javax/xml/parsers/DocumentBuilderFactory - this is a java-standard class, it comes with the j2se and it shouldn't be a problem to find it for the java interpreter from sun! I don't know nothing about websphere, so i can't help you to make it work with that.
amitinus
Greenhorn

Joined: Jul 01, 2001
Posts: 6
Thanks Tobias,

I will try that when i get back to work. Now, let me clear one thing.. even though my initial path setting did mention c:\j2sdk... etc there is no jesdk1.4 installed on my system drive at work. I dont know why the system admin people set the default path this way.. and its standard installation and every one must have the same one.. then with websphere there are various versions of javac.exe and java.exe's under various directories.. like under base_v5, under aes_v5, under express_v5 etc... and under eclipse\jre... the point is I have tried using the same run command with my class path properly set, and having just one instance of JDK 1.4.1 installed on my home machine, win 2000 OS. SO why do i still get the same error message. I have Java installed from sun, j2sdk1.4.1 and you are right in pointing that there is problem with the class javax/xml/parsers/DocumentBuilderFactory.

Is there anything i can tweak, like replace their .jar distribution with the standard xerces.jar or some jar which has that class implementation , which is provided by sun or apache etc ? what do you think ? At my work place I have to get done with one version of java compiler. All I can do is as a last effort, try changing the path to each one of the java compilers and then try running "run.. commmand" and see if it works (by luck) against any specific java compiler. Did you try to download and work through the PMD installation, if you have a ready JDK installation.. If you are facing the same problem. then theres something funcamentally wrong about the class, as you said. Yet do let me know if you see some good result, and not an exception...

Thanks again.. I will let u updated once i find a solution to this problem. Not many people seem to have used profilers like PMD..

more later
Amit


Delete this lines, and see if it works:

REM adding a path to Java compiler

set JAVA_HOME=F:\Program Files\IBM\Websphere Studio\runtimes\base_v5\java\bin
set path=%JAVA_HOME%;%path%


Wich java are you using? Looks like you have two installed, one with websphere and one with the j2sdk.

The problem is, that your profiler can't find the class javax/xml/parsers/DocumentBuilderFactory - this is a java-standard class, it comes with the j2se and it shouldn't be a problem to find it for the java interpreter from sun! I don't know nothing about websphere, so i can't help you to make it work with that.
 
 
subject: Intermediate/Advanced Java Users. Pl help with static profilers (PMD)
 
Similar Threads
PMD tool
Issue when to trying check code with pmd
Ruleset not found error while customizing PMD.
How to test with PMD in Eclipse
PMD for android