How to check code coverage with JaCoCo agent? - java

I am going to deploy application with JaCoCo agent to production environment to let it work for some time. The result should help me identify the parts of code I can get rid of.
I started some research around the topic and prepared HelloWorld application:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compiled the class: "javac HelloWorld.java" and got HelloWorld.class file.
Now I run the app with the following command: "java -javaagent:jacocoagent.jar HelloWorld" the program executes and jacoco binary is generated. The file contains some binary data.
Everything looks fine but the coverage report shows 0% coverage although it should be 100%.
Has anyone faced this issue or correct me what I am doing the bad way?

I generated full report using this steps. Since I use maven for this kind of operations I added maven after your steps. I created HelloWorld.java just copying from your question. Then I follow these steps:
javac HelloWorld.java which outputs HelloWorld.class
Then I created jacoco.exec by executing java -javaagent:jacocoagent.jar HelloWorld
Then I created a pom.xml file which contents are like this.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test</name>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
</plugin>
</plugins>
</build>
After that I created a target/classes directory. I copied jacoco.exec to target/ and HelloWorld.class to target/classes.
Then I executed mvn jacoco:report which generates a report to target/site/jacoco. Which contains correct coverage information.
I know using maven may not sound good for a simple application. But I don't know any other way to generate reports from jacoco.exec. By the way your maven plugin version and jacocoagent version must match.
And here the result I get.

Related

Using Maven over bash shell to automate my deploy workflow (github-package-registry)

I am correctly deploying my JAR file using the command line below, however I would like to create a pom.xml file and invoke mvn deploy or something similar to deploy this package to github packages.
Is this possible? How can I do it?
mvn deploy:deploy-file -DgroupId=com.soma -DartifactId=my-module \
-Dversion=1.0.0 -Dpackaging=jar -Dfile=my-module.jar \
-DrepositoryId=github \
-Durl=https://maven.pkg.github.com/$USER/my-artifacts
In other words, I prefer to use Maven to automate my workflow over bash shell.
I appreciate any help.
The following minimal pom.xml should work by executing mvn deploy with
the following caveats:
Replace $USER with your actual GitHub user ID
Put your JAR in target/my-module-1.0.0-SNAPSHOT.jar
Make sure your ${user.home}/.m2/settings.xml has a <server/> entry
with your GitHub user and token
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soma</groupId>
<artifactId>my-module</artifactId>
<version>1.0.0</version>
<distributionManagement>
<downloadUrl>https://maven.pkg.github.com/$USER/${project.groupId}/</downloadUrl>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/$USER/${project.groupId}/</url>
</repository>
</distributionManagement>
</project>
This should be straightforward if you already have a Maven build for the
project.
(I have run into problems with GitHub throttling requests when trying to
deploy multimodule projects or quickly redeploying an artifact.)

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Module

new user here!
I know there's similar questions with answers to this, but I don't know how to apply them to my case, so sorry if it's repetitive!
So... I'm trying to make my first bot for Telegram and I've decided to use Java. I'm following this tutorial and copypasted the code from the two example classes (EchoBot and Main). The only thing I changed is the token with the token I got from the BotGodfather on Telegram.
I'm using Eclipse on Ubuntu 18.04.1 as IDE so I started by making a Java project and then configured it as a Maven project. This is the code of my pom.xml:
`
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EchoBot</groupId>
<artifactId>EchoBot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EchoBot</name>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
`
When I run the program I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Module
at org.telegram.telegrambots.ApiContext.getInjector(ApiContext.java:46)
at org.telegram.telegrambots.ApiContext.getInstance(ApiContext.java:25)
at org.telegram.telegrambots.bots.TelegramLongPollingBot.(TelegramLongPollingBot.java:17)
at pearlbot.EchoBot.(EchoBot.java:8)
at pearlbot.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Module
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 5 more
What could the problem be?
Keep in mind that I don't even know what Maven is, so if it's something related to it, you'll need to explain what's wrong as if you were talking to a child! ^^''
I got this error when i try to install testng for eclipse Version: 2019-12 (4.14.0) and run the program then i got this error. Finally i got fixed -
Eclipse IDE for Java Developers, Version: 2019-12 (4.14.0), Build id: 20191212-1212.
TestNG 7.1.0.r202001120626
Removing the TestNG library from the build path of the project containing the test and installing TestNG from menu Help / Install New Software did not work for me, I kept getting this error.
What worked for me was downloading guice-4.2.2.jar (from https://github.com/google/guice/wiki/Guice422), copying it into any folder, and adding it to the build path of the project as external JAR.
From Eclipse, Go to Help > Install software or You can install from market place as well (Help > Market Place). After installing TestNG from market place, it dint work, but installing the GUICE422.jar to the build path worked for me
So make sure after installing TestNG from market place, install the Guice422 Jar file as well link to install testng from eclipse market place
This worked for me --- Hope it helps

Similar solutions for java.lang.NoClassDefFoundError: org/joda/time/DateTime not working

I have seen this question posted here before and I have looked at the solutions however I cannot fix the problem I'm having. I created a very simple Maven project in Eclipse for Java and I want to run the output jar file e.g. java -jar jarfilename.jar
I can run the program by right clicking on the project in eclipse and indicating run as Java application. I can build the project to a jar file with mvn package. Running the jar file I get the output of NoClassDefFoundError for the joda time. The joda jar files are in the configured repository e.g. .m2/repository/joda-time/joda-time/2.8.2. There are no errors indicated for the project in Eclipse. I'm using jdk1.8.0_92 Maven version 3.3.9 and eclipse Java EE Neon release 4.6.0. Java home is configured in the environment variables and so too is the class path as: ...\Java\jdk1.8.0_92\jre\lib;C:\Users\username.m2\repository
Some additional information the classpath is correct in terms of not having typos in it. I also looked at a solution from another similar question wherein the suggestion was to add the external jar to the bootstrap entries under run configuratotion. I have also made an entry in the Java build path for joda time which points corretly to the .m2/repository.../joda-time/2.8.2 What this seems like is that when this runs from eclipse the path to the joda time jar file is (for lack of a better term) known. When the jar file is built however that path is not known. I opened the jar file and looked at the MANIFEST.MF file and I see:
Manifest-Version: 1.0
Built-By: John
Class-Path: joda-time-2.8.jar
Build-Jdk: 1.8.0_92
Created-By: Maven Integration for Eclipse
Main-Class: hello.HelloMain
The source is very simple:
package hello;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
public class HelloMain {
public static void main(String[] args) {
System.out.println("Hello From My Main ! It worked\n");
final DateTime today = new DateTime().withZone(DateTimeZone.UTC);
DateTime tommorrow = today.plusDays(3);
String startTime = today.toString(DateTimeFormat.forPattern("yyyy-MM- dd'T'HH:mm'Z"));
String endTime = tommorrow.toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'Z"));
System.out.printf("The start time %s End Time %s \n", startTime, endTime);
}
}
This is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>hello.HelloMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8</version>
</dependency>
After trying many different approaches to the Pom file I had settled on a different approach. This would only apply if your using Eclipse to do this as Eclipse offers an Export to runnable jar feature. While in Eclipse highlight your project of interest, right click to Export and select Java/Runnable JAR file. Click next and select "Package required libraries into generated JAR". This built the jar file with all of the required jar files I needed. While this really does not solve the problem from the perspective of creating the output jar using the maven packaging it did provide a proper executable jar file.

Maven project giving error

After setting up the project in intellij as a maven project like so ..
I set up my pom.xml file with a basic structure ...
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany.test</groupId>
<artifactId>jreddit-testing</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jreddit-testing</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.github.jreddit</groupId>
<artifactId>jreddit</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
when I run sudo mvn clean install it installs everything with no errors.
On running the project I get the Error ..
Error:java: directory not found:
/Users/stingRay/Documents/workspace/jreddit/target/generated-sources/annotations
Not sure if this will solve your issue but the comments have not enough space :)
I would argue the environment variables are simply not properly configured. I recommend not using sudo and maven. It is simply not necessary if everything is configured properly.
The download page of maven contains the instructions what to do: http://maven.apache.org/download.cgi (scrolling down)
In short:
Download the Java Development Kit (I don't use the packet manager from Linux for this, to prevent having root being able to execute Java commands or run servers accidentally). It looks you are working on MacOS? So Download the dmg file from oracle.
Download the Maven archive and extract it (it can also be in a shared dirctory so it is readable for every user on the system)
the important part: setting the environment variables. You must set JAVA_HOME, M2_HOME, optionally MVN_OPTS and the PATH for convenience - so "mvn" runs in your terminal without sudo.
There are tutorials on the web that guide you through this, like this one: http://bitbybitblog.com/environment-variables-mac/
If you work with IntelliJ or the terminal the .bash_profile file be enough. Editing the .plist file is for OSX internal tools only. In IntelliJ it will work once the M2_HOME variable is set (if I remember correctly) and IntelliJ is restarted.
Note you may need to clean up the files already around (to make sure your user can write them without sudo). Meaning:
/Users/stingRay/Documents/workspace/jreddit/target/
/Users/stingRay/.m2/repository (or where your local maven repo is located)
Hope that will work :)

Eclipse NoSuchMethod exception; configuring build path

I have a maven project which actually builds as multiple java projects. Project B contains a class which is a child of a class sin Project A. When I try to run this class in a debugger I get a NoSuchMethod error when the method tries to call any functionality from it's parent.
The Maven setup is designed to compile every single project and place it in the maven repository so other projects can find them (it has a sense of dependency so it builds pre-req projects firsts). This is all good for deployment, but I don't want to force people debugging in eclipse to do a maven install every time the start up their debugger. Instead I tried adding pre-req projects to the class path of the applicable projects (build path -> add class folder). This doesn't work. I think it's due to having both the maven repository and the class path in my build path? but the class folders should be parsed forced and the newer class folders should be parsed before the maven install right?
How can I configure this to work without needing to re-do a maven install each time?
What exactly do your pom files look like? I often use a setup where I have one parent Maven project that is comprised of other Maven sub projects.
I will have a parent pom that looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>put group id here</groupId>
<artifactId>name of artifact id</artifactId>
<version>0.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>example-subproject-1</module>
<module>example-subproject-2</module>
</modules>
</project>
I'll then have pom files in the subprojects that look like this (assume this is the pom file for subproject 1):
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<artifactId>artifact id of parent pom</artifactId>
<groupId>group id of parent pom</groupId>
<version>version number of parent pom</version>
</parent>
<groupId>group id</groupId>
<artifactId>artifact id</artifactId>
<packaging>packaging</packaging>
<version>1</version>
<name>put name here</name>
<dependencies>
<dependency>
<groupId>group id of example subproject 2</groupId>
<artifactId>example-subproject-2</artifactId>
<version>version number of example subproject 2</version>
</dependency>
</dependencies>
</project>
When I have things set up this way, I can compile the entire project if I'm in the root directory. If I just want to compile a specific directory, I just change into that directory. Make sure to include any needed subprojects in your dependencies section.
Make sure you have the m2eclipse plugin installed.
http://eclipse.org/m2e/download/
Also, install the m2eclipse-wtp plugin
http://marketplace.eclipse.org/content/maven-integration-eclipse-wtp#.UUdUhBxwrIU
Make sure you have created your projects as 'Maven' projects. So, if you look at your 'ProjectA->Properties->Builders', you should see a 'Maven Project Builder'.
If you follow all above steps, your project dependencies should resolve correctly and you should not have errors.
If you still have errors, pls post your eclipse project structure.

Categories

Resources