How to execute a sample java program which is instrumented using clover ant, through command line.

Hi All, This is Sagar. I am doing a POC of clover with a sample java code something similar to how an eclipse plugin works.

Have created an instrumented code using clover ant and got the jar files created.

Q1) Not able to run the jar files. Its giving below exception in the commandline:

C:\Users\sv186037\workspace\CloverInstrumentBuild>java -cp C:\Users\sv186037\workspace\CloverInstrumentBuild\build\jar\CIB.jar com.cib.CIB
Exception in thread "main" java.lang.NoClassDefFoundError: com_atlassian_clover/TestNameSniffer

at com.cib.CIB.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com_atlassian_clover.TestNameSniffer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

When I have opened one of the instrumented classes I see clover warning below :

[CLOVER] WARNING: The Clover version used in instrumentation does not match the runtime version. You need to run instrumented classes against the same version of Clover that you instrumented with. 

[CLOVER] WARNING: Instr=4.1.2#20161011084623935,Runtime=

C:\Users\sv186037\workspace\CloverInstrumentBuild\.clover\clover4_1_2.db

Q2) coverage information is created in the same folder of coverage.db file when the code is executed in eclipse. But will the same way work when we try to execute the code using commandline ?

1 answer

0 votes

 

Hey,

The NoClassDefFoundError definetely means you haven't provided clover.jar to the classpath. You should probably execute Java via 

 

java -cp  PATH_TO_CLOVER_JAR;PATH_TO_YOUR_APP_JAR MAIN_CLASS

 

 

 

When I have opened one of the instrumented classes I see clover warning below :
I have troubles understandig what exactly do you mean by that.

  • Have you simply opened the Java bytecode and you saw it?

    Clover adds a code generating a warning message in case of versions missmatch (between instrumentation and runtime phases). So it's going to be always there, I don't think you should be worried about that. 

  • Have you saw this in the logs after running instumented code?

    This means that you actually provided different Clover version to the classpath than the one you've used to instrument sources. You shall use the same clover.jar for both phases. 

 

Q2

coverage information is created in the same folder of coverage.db file when the code is executed in eclipse. But will the same way work when we try to execute the code using commandline ?

This is true. Clover dumps coverage data to the same directory where Clover database lies. Plase note that Clover by default flushes all coverage during graceful JVM shutdown (it adds handler to the JVM shutdownhook), which means that if you kill JVM there might be some of the coverage data missing.   

 

 

Hey Grzegorz,

Thanks for the response, adding class path for cloverjar;myappjar worked for me and each time I run it from commandline I see coverage getting created. Really excited to see :)

 

Now my actual idea to start with POC is like, if I try to run a java code with some Python test, do we get the same coverage info created or do we need to add any tags similar to junit ?

 

run a java code with some Python test

 

I'm not sure what does it exactly mean, but generally Clover is a JVM tool and it's able to instrument only Java and Groovy code, thus calculation of coverage data is not possible for other languages than those two. So if you want to execute Java via some Python scripts then I think eveything should work (just remember about Clover initstring and classpath cofiguration). However, if you have some mixed Java-Python project I can't see Clover gathering coverage data for Python side. 

We are on the same page Grzegorz, we want the code coverage for Java code only. Python tests are just the way we are hitting or running the code.

Coming to the initstring and classpath configuration : We are planning to hit/run the java code using PyTest framework, so do you have any settings inspecific to that like how its mentioned for Junit.  

Generally speaking, Clover in runtime needs at least initstring System property which configure location of Clover database. 

So, in case you have some server application and your PyTests are some kind of Selenium or REST tests which are executed against the app. Just make sure you start the server with proper Clover settings.

In case PyTests somehow execute Java code, well.. to be honest it's a completely new solution I've never heard about. However, if you configure the tests to invoke JVM with proper System configuration, I think it should work just fine. 

Hi Grzegorz,

Have given the init string as a different folder other than .clover/coverage.db, and it worked... its creating the coverage in the new folder.

Q1 : Want to run the jar/my_app in a Linux ubuntu machine and want the coverage info to be updated in the windows machine.

Can you help me with the syntax of what the initstring=????????

Q2 : How to give the init string value through command line, tried through commadline java -cp CLOVER_JAR;INSTRMNTD_APP_JAR -Dclover.initistring

doesnt seem to be a wright syntax.... 

Want to run the jar/my_app in a Linux ubuntu machine and want the coverage info to be updated in the windows machine.

What do you mean by coverage info ? You can simply generate report on Linux machine and copy the report to Windows one. initstring won't accept remote location, unless there's a simple network hard drive mounted in the Linux machine. 

 

Q2 : How to give the init string value through command line, tried through commadline java -cp CLOVER_JAR;INSTRMNTD_APP_JAR -Dclover.initistring

This is the right syntax, but make sure you're not putting system properties AFTER main class because in that case they will be trated as program arguments. 

Grzegorz, 

I am trying to run the instrumented jar in Linux, which is generate using ant in the same Linux machine.

But I dont see the coverage.db getting updated.

I have copied the sample java project from windows, clover.jar and clover.license

I have set the new paths of clover.jar and coverage.db using intistring, using the clover.jar class path while running the jar.

I dont see the coverage.db file updated, with supporting files...

which are supposed to be created like in windows.

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published yesterday in Jira Ops

Jira Ops Early Access Program Update #2: Let’s talk severity levels

Welcome to your weekly Jira Ops Early access program update, where we’re sharing news and updates on Jira Ops' progress as we work toward our 1.0 release. If you ever want to drop us feedback or idea...

25 views 0 0
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you