Cobertura data file not getting updated - java

I am trying to find out functional code coverage for my JAVA classes.
My java classes are deployed on a server as a JAR file. (Note: It is not deployed in any application server such as tomcat). After deploying my JAR file to server I instrumented that jar file using ant task. Classes inside my jar file are now instrumented (I confimred that by decompiling the classes). When I run my application (which in turn will access my classes from JAR file) it should created a new data file (.ser file) and add code coverage information into it. But No new data file is getting created neither is old data-file getting updated.
My project structure on server is as follows
Main-Folder
|--cobertura-2.1.1
|--ccbuild.xml
|--cobertura.ser (created during instrumentation)
|--lib
|---code.jar (This file contains my instrumented classes)
|---other-dependency.jar (All other dependencies)
|---cobertura.jar
Why are my class files not generating code coverage information?

I had this problem while generating .ser file. My problem resolved after changing the version of one dependent lib commons-io to V2.4

Related

Deployed spring boot jar file to Docker - how to access and change static resources

I'm kinda new to spring and web development as a whole.
My question is:
When you build a spring boot project (using Maven) into jar file and deploy it via Docker, everything is in one jar file. How can you access your resources (css, js, images, html...) if you want to edit something? Like change something in css file or add something to html page. Is it even possible? Or do you have to build a new jar file everytime, when you need to change something (in frontend)? Also, when there are being uploaded some images or other files, where are they stored? This stuff is very confusing for me and i can't find any related books or help at all.
Thanks for help!
when you package any java program it is nothing but a zip file. Based on what kind of package it is, you wither name it as a Jar or War.
Jar == Java archive
War == Web archive
Now, given the fact that jar and war both are essentially a zip archive, it gives you flexibility to extract and modify them just like any other zip file.
On windows, I think softwares like 7zip let you update the jar inline. I have done it multiple times, especially when I wanted to change application.properties alone on cloud machines, and no other code changes were required. In such cases, building the whole jar and transferring it again to cloud machine could be time consuming. So I would just extract the contents, update whatever I want to, and rezip the package.
Here is the commands you can use -
jar xf jar-file
This should extract the files into a directory.
This SO thread will guide you towards creating jar files.
Something like jar cf myJar.jar ** should be enough to generate a jar file IMO, but syntax might vary.
The jar file is actually just a zip file containing all the files and classes of your application, so technically you can change files in it like any other zip archive. Best practice is to build the jar file using Maven or Gradle from source every time you need something changed.
It's good practice to keep the source in version control using Git, and tag each build in the git repository - that way you can easily keep track of changes to the jar file by looking at what's in git at the time of the build.

I created an installer via install4j for my app using a jar generated from maven. Why can't it find the main class when executing the .exe?

Here's how I created a jar file using maven.
​
Now for my JavaFX Application, I'm using afterburner FX Framework. Now I need to create an installer for this app to be deployed to other devices. I'm using Install4j. My steps:
mvn clean package
copy and paste the generated jar file into a different directory
add that directory to install4j Files
on Launcher under Java invocation, I select the jar file, and then I select my main class: `BOOT-INF.classes.inc.pabacus.TaskMetrics.TaskMetricsApplication`
I Build the installer and run it, install to Program files, and then open the exe file
But then an error dialog shows up:
java.lang.NoClassDefFoundError: BOOT-INF/classes/inc/pabacus/TaskMetrics/TaskMetricsApplication (wrong name: inc/pabacus/TaskMetrics/TaskMetricsApplication)
So what I did wrong was two things:
Initially, like way way back, I tried using JavaFX with Spring Framework - unsuccessfully. I'm no longer using Spring, but I still had some leftover Spring in my pom file, which caused it to put the files in a BOOT-INF directory when i package it to jar. I just simply had to remove the Spring leftovers, and the boot-inf directory was gone.
So in install4j, you select a directory that would contain the files you would add to your installer. In the tutorials, they had a separate lib directory which contained external libraries. So I thought that's all I needed. I copied my dependencies into a lib folder via maven, then i put them into a directory along with my jar. So that's all my directory had - the jar file and the lib folder. That doesn't work. I didn't know. Apparently, it needs all the files inside the target folder generated by maven. I should've just used the target folder itself.
So there you have it. I have now successfully created an installer. I do hope no one walks as silly as me, but if you had also encountered the same mess up, well... here ya go.
You seem to have configured
BOOT-INF.classes.inc.pabacus.TaskMetrics
as the main class when the correct package name is
inc.pabacus.TaskMetrics.TaskMetricsApplication
Alternatively, your VM parameters configuration for the launcher is incorrect and includes text that can be interpreted main class.

The jar file is not reflecting any changes after making changes in code

I'm working on a java app. I created a jar file and after that I made some changes in my code about look and feel of app using Net-beans but the changes I made are not reflecting in the jar file. So do I have to delete the old jar file and then create a new one?
You need to re-make you jar file again, the jar file is essentially a compressed file containing all the resources (classes, images, etc) required to run your program. So if any of your classes changed you need to recreate the jar file to ensure the updated classes are incorporated.

(Tomcat 7) Deployed jar not finding files

I deployed a war file onto a Tomcat 7 instance running on a remote Linux machine and I'm getting FileNotFoundExceptions.
One of the referenced jars in the project, which contains code that I did not write, uses several files (which I have included, but it is not finding). These files are located in the classes folder. It appears the classpath I have set for the project is being ignored by this jar. These files that it uses, e.g. .properties files are external to the jar.
Here is an example of how it is invoking the files:
FileOutputStream fos = new FileOutputStream("Key.ser");
I was getting these errors when developing the source project in Eclipse. I was able to configure the project to tell it where to find these files via Run Configurations -> Arguments -> Other but the exported .war file appears to not have this bundled with it, only the source project has it. Now I'm seeing them again when trying to deploy the application to Tomcat on another server via war file.
How do I configure the deployed jar file in the deployed Tomcat 7 webapp to find these files that the jar uses? I am loathe to change the code since I did not write it so am really hoping to avoid this.
I am able to get this to work on a local Tomcat 7 running on Windows instance integrated with Eclipse as explained earlier so I'm wondering if maybe this can be duplicated?
You will not be able to find the file by simply referencing the file name using FileOutputStream. You are correct to place the file in the 'WEB-INF/classes' directory, which will allow it to be located on the classpath.
To load the file, you need to load it as a classpath resource using something similar to this:
String classpathLocation = ""Key.ser"";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or if you want it as an inputstream:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);

What is the difference between getResourceAsStream("Words.txt") and FileInputStream("./src/package/Words.txt")? [duplicate]

This question already has answers here:
getResourceAsStream() vs FileInputStream
(6 answers)
Closed 7 years ago.
I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1, SO2). But I have tried FileInputStream("./src/package/Words.txt") and the text file can still be successfully loaded.
What are the differences? And why is the method getResourceAsStream encouraged?
At the moment, you're on your developer workstation, and are probably running your app from your IDE. Tomcat happens to be started from the IDE project root directory, and thus using
new FileInputStream("./src/package/Words.txt")
allows reading the file stored in your project src directory.
But that's not how the project will be run in production. In production, you'll have a Tomcat server started from a totally different directory, using a shell script. And the production server won't have the source project at all. All it will have is Tomcat, and the war file constituting the artifact built from the project.
So there will be no src directory at all, and the file Words.txt won't even be anywhere on the file system. It will only be en entry of the war file (which is in fact a zip file), located under WEB-INF/classes/package along with the .class files produced by the compiler from your Java source files.
So, in order to be able to read that "file", you can't use file IO: the "file" doesn't exist in the file system. You need to use the ClassLoader that will locate the "file" inside the war file and load it from there.
That will also go fine during development, when the app is run from an exploded war structure: the class loader will find the class under the target directory used by your IDE to store the class files and resource files.
Note that what you need to load that resource, if it's in the package com.foo and MyClass is in that same package, is
MyClass.class.getResourceAsStream("Words.txt")
or
AnyOtherOfYourClassesWhateverThePackageIs.class.getResourceAsStream("/com/foo/Words.txt")
or
classLoader.getResourceAsStream("com/foo/Words.txt")

Categories

Resources