Direct use of Java analyzer from command line
- How to install the Java analyzer core
- Arguments of the Java analyzer core
- How to run the analysis
- Java analyzer configuration file
- Global Java analyzer settings file
- The return codes for the Java analyzer core
- How to update PVS-Studio Java
The scenario where Java analyzer is run directly by invoking its core is an advanced use case and is not recommended as the primary method. For convenient integration and use of PVS-Studio Java analyzer, we recommend using:
- the Gradle build system plugin;
- the Maven build system plugin;
- the plugin for IntelliJ IDEA and OpenIDE;
- the Visual Studio Code extension.
How to install the Java analyzer core
On Windows, use the PVS-Studio installer to install the Java analyzer core. You can download the installer on the Download PVS-Studio page.
Also, regardless of what OS you are using, you can download the ZIP archive for Java on the Download page. The archive contains the Java analyzer core (a folder named 7.42.105102 in the pvs-studio-java directory). Unpack the Java analyzer core to the path you need or to the standard installation directory for the Java analyzer core:
- Windows:
%APPDATA%/PVS-Studio-Java; - Linux and macOS:
~/.config/PVS-Studio-Java.
Arguments of the Java analyzer core
To get information on all available arguments of the analyzer, run the --help command:
java -jar pvs-studio.jar --help
Analyzer arguments:
--additional-warningsspecifies a list of diagnostic rules that will be added to the analysis. It has a higher priority than--enabled-warnings,--disabled-warnings, and--analysis-modesettings. Default value: not set. Use whitespace to separate multiple rules. The example:--additional-warnings V6001 V6002 V6003.--analysis-modespecifies a list of enabled diagnostic rule groups. It has a lower priority than--enabled-warnings,--disabled-warnings, and--additional-warningssettings. Default value:GA. Use whitespace to separate multiple groups. The example:--analysis-mode GA OWASP. Available groups:GA(general analysis diagnostic rules);OWASP(rules based on OWASP Top Ten and OWASP ASVS).
--analyze-only-listspecifies a path to the text file that contains a list of paths to files and/or directories for the analysis—each entry must be on a separate line. Relative (will be expanded relative to the working directory) and absolute paths are supported. Files and/or directories read from the file specified in this argument are merged into a common list with values from the--analyze-onlyargument. This setting has a lower priority than--exclude. Default value: not set.--analyze-onlyspecifies a list of files and/or directories for the analysis. Relative (will be expanded relative to the working directory) and absolute paths are supported. Files and/or directories passed in this argument are merged into a common list with files and/or directories from the--analyze-only-listargument. If no value is specified for this setting, all files will be analyzed. The setting has a lower priority than-- exclude. Default value: not set. Use whitespace to separate multiple files or directories. The example:--analyze-only "path/to/file1" "path/to/file2" "path/to/dir".--cfg(-c) specifies a configuration file for running the analyzer core. This file stores the values of Java analyzer core command line arguments in JSON format. Read more information about this file in the documentation. Default value: not set.--convertruns the analyzer in the conversion mode. Available modes:toFullhtmlconverts a warning report to the.fullhtmlformat;toSuppressconverts a warning report to a suppress file.
--disable-cachedisables caching of the program metamodel. When caching is disabled, the project model is not cached and is rebuilt each run. Incremental analysis is also disabled, even if the--incrementalflag is specified. Default value:false.--disabled-warningsspecifies a list of diagnostic rules that are disabled during the analysis. If this setting is not specified, all diagnostic rules are considered enabled. This setting has a higher priority than--enabled-warningsand--analysis-mode, but has a lower priority than--additional-warnings. Default value: not set.--dst-convertspecifies a path to the file or directory where the conversion result will be written—a file fortoSuppress, a directory fortoFullhtml.--enabled-warningsspecifies a list of enabled diagnostic rules. During analysis, only the diagnostic rules from this list will be used. This setting has a lower priority than--disabled-warningsand--additional-warnings, but a higher priority than--analysis-mode. Default value: not set. Use whitespace to separate multiple groups. The example:--enabled-warnings V6001 V6002 V6003.--excludespecifies a list of files and/or directories to be excluded from analysis. Relative (will be expanded relative to the working directory) and absolute paths are supported. This setting has a higher priority than--analyze-onlyand--analyze-only-list. The setting has no default value. Use whitespace to separate multiple files or directories. The example:--exclude "path/to/file1" "path/to/file2" "path/to/dir".--ext-filespecifies a path to the file with classpath. The setting has no default value. The classpath separator is:on*nixsystems and;on Windows systems. The example:--ext-file "path/to/project_classpath_file".--ext(-e) defines classpath. The setting has no default value. Use whitespace to separate multiple classpath entities. The example:--ext "path/to/file.jar" "path/to/dirJars".--fail-on-warningssets a non-zero return program code if the analyzer issues at least one warning for the project code. This behavior can be useful when integrating into CI/CD. Default value:false.--force-rebuildforces a full rebuild of the cached program metamodel. When this flag is used, incremental project analysis mode is disabled (--incremental). Default value:false;--help(-h) outputs help information about Java analyzer core arguments to the console.--incremental(-i) enables incremental analysis. In this mode, the analyzer checks only modified files. Default value:false.--java-pathspecifies a path to thejavaexecutable file that is used to run the analyzer core. This setting can be configured system-wide in theglobal.jsonfile. Ifjava-pathis not used, PVS-Studio attempts to use the path from thePATHenvironment variable.--loggingspecifies the logging level when starting the analysis. When enabled, log files are created for the current run in the.PVS-Studio/logssubdirectory relative to the directory where Java analyzer is currently running. If an invalid value is passed, logging will be disabled. Default value:OFF. Available values are:OFFERRORWARNINFODEBUGTRACEALL.
--output-file(-o) specifies a path to the analyzer report file. The report format does not depend on the file extension specified in the parameter. Default value:./PVS-Studio+ the format extension defined by--output-type. To generate a report in.fullhtmlformat, specify the directory where thefullhtmlsubdirectory containing the analyzer report file (index.html) will be created. Default value:./fullhtml. Note. It is recommend to use the PlogConverter (Windows) and plog-converter (Linux and macOS) instead of--output-file. They enable converting the analyzer report to more formats (for example, SARIF). The utilities provide additional features: filtering warnings from the report, converting paths in the report from absolute to relative (and vice versa), comparing reports, etc.--output-type(-O) specifies the analyzer report format. Default value:json. Available values are:textlogjsonxmltasklisthtmlfullhtmlerrorfile.
--security-related-issuestags warnings related to potential security issues with an additional mark up in theSASTfield. Default value:false.--sourcetree-rootspecifies the root part of the path that the analyzer uses to generate relative paths in the report for warnings. Default value: not set. The example:--sourcetree-root /path/to/project/directory.--src-convertspecifies the path to the analyzer report that contains warnings (*.json);--src(-s) specifies the set of files or directories of the analyzed project. The setting has no default value. Use whitespace to separate multiple files or directories. The example:--src "path/to/file1" "path/to/file2" "path/to/dir".--suppress-basespecifies a path to the suppress file that contains suppressed analyzer warnings. These warnings are excluded from future analysis reports. Default value:./.PVS-Studio/suppress_base.json.--threads(-j) specifies the number of analysis threads. This setting can also be configured system-wide in theglobal.jsonfile. Default value: the number of available logical processors.--timeoutspecifies the timeout for analyzing one file (in minutes). This setting can be configured system-wide in theglobal.jsonfile. Default value:10.--activate-licensesaves the license information specified via the--user-nameand--license-keyarguments to a file. Default value:false.--license-keyspecifies the license key.--license-pathspecifies a path to the license file. Valid file extensions are.xml,.lic. The default value is%APPDATA%/PVS-Studio/Settings.xml(Windows) or~/.config/PVS-Studio/PVS-Studio.lic(macOS and Linux).--user-namespecifies the analyzer username associated with the license key.--compatibilityenables the V6078 diagnostic rule, which detects potential API compatibility issues between selected Java SE versions. Default value:false.--exclude-packagesspecifies the packages to be excluded from compatibility analysis. The V6078 diagnostic rule uses this setting if the--compatibilitysetting is enabled. The example:--exclude-packages "package1" "package2" "package3".--source-javaspecifies the Java SE version on which the analyzed application is developed. The V6078 diagnostic rule uses this setting if the--compatibilitysetting is enabled. Minimum value:8. Maximum value:14.--target-javaspecifies the Java SE version against which the API used in the application (--source-java) is checked for compatibility. The V6078 diagnostic rule uses this setting if the--compatibilitysetting is enabled. Minimum value:8. Maximum value:14.
How to run the analysis
Before running the analysis, enter the PVS-Studio license. To learn how to do this, please consult the documentation.
The quick start example of the Java analyzer core:
java -jar pvs-studio.jar -s A.java B.java C.java -e Lib1.jar Lib2.jar -j4
-o report.txt -O text --user-name someName –-license-key someSerial
java -jar pvs-studio.jar -s src/main/java --ext-file classpath.txt -j4
-o report.txt -O text --license-path PVS-Studio.lic
Note.
- The analyzer needs a collection of source files (or directories with source files) to perform the analysis and the classpath to build the program metamodel correctly.
- When analyzing a project, the plugin runs the Java analyzer core, which uses the Java version from the
PATHenvironment variable by default. You can set a different version by configuring thejavaPathplugin. The Arguments of the Java analyzer core section above describes this in detail.
How to change the Java version to run the analyzer
By default, the analyzer starts the core with java from the PATH environment variable. If you need to run the analysis with some other java, you can set it manually. To do this, run the Java analyzer core by using the full path to the java file from the JDK. The version of this JDK will be used when checking the source code of the project:
/path/to/jdk_folder/bin/java -jar pvs-studio.jar ^
-s A.java B.java C.java -e Lib1.jar Lib2.jar -j4 ^
-o report.txt -O text --user-name someName –-license-key someSerial
Java analyzer configuration file
To simplify the command for running the analysis, you can put the command line arguments into a special JSON file. Later this file can be passed to the analyzer core through the --cfg flag.
The syntax of the file is as follows:
{
"single-value-parameter": "value",
"multiple-values-parameter": ["value1", "value2", "value3"]
}
Each parameter in the configuration file is the full name of a command line flag with the value to be passed in that flag.
The example of a configuration file:
{
"src": ["A.java", "B.java", "C.java"],
"threads": 4,
"output-file": "report.txt",
"output-type": "text",
"user-name": "someName",
"license-key": "someSerial"
....
}
In this case, you can run the analyzer with the following line:
java -jar pvs-studio.jar –-cfg cfg.json
Note. Parameters passed via the command line have a higher priority than parameters specified in the configuration file.
Global Java analyzer settings file
The Java analyzer core takes some settings from the global.json file. This file is located at the default installation path of the Java analyzer core:
- Windows:
%APPDATA%/PVS-Studio-Java/global.json; - Linux and macOS:
~/.config/PVS-Studio-Java/global.json.
The list of the settings:
java: the default value isjava.jvm-arguments: the default value is["-Xss64m"].threads: the default value isthe number of available processors. This value can be overridden via the--threadscommand line argument of the Java analyzer core.timeout: the default value is10. This value can be overridden via the--timeoutcommand line argument of the Java analyzer core.verbose: the default value istrue.
By default, these values are used for all Java analyzer cores in the system as well as for PVS-Studio Java plugins. If necessary, you can change the values of these parameters. For example, to ensure that all PVS-Studio plugins for Java utilize the same number of threads for analysis.
You can read more about which of these parameters can be changed in the following documentation sections:
The return codes for the Java analyzer core
0: the analysis is completed. Errors can be both detected and undetected.50: an error occurred during analysis.51: invalid arguments are passed when starting the analysis.52: the invalid or expired license is used.53: the analysis is completed. The analyzer detected potential errors in the project code. This return code will only be returned when the--fail-on-warningsJava analyzer core flag is enabled.54: an attempt was made to access features available under the Enterprise license.
How to update PVS-Studio Java
When you run the analyzer, it checks whether a new version of PVS-Studio analyzer is available. If a new version of the analyzer has been released, the file with the analysis results contains the following message: "A newer version of PVS-Studio is available (7.42.105102)". This message contains the latest version of the Java analyzer core.
You can download the latest version of PVS-Studio from the file at the link.
To update the Java analyzer core, download the ZIP archive for Java on the Download page. The archive contains the Java analyzer core (a folder named 7.42.105102 in the pvs-studio-java directory). Unpack the Java analyzer core to the path you need or to the standard installation directory:
- Windows:
%APPDATA%/PVS-Studio-Java - Linux and macOS:
~/.config/PVS-Studio-Java
This process can be automated with various scripts to ensure that the latest version of the Java analyzer core is used.