How to load a jira core api class dynamically?

Hello Atlassian Team,

I am unmarshaling a json string element containing the following fully qualified class name:

com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction

I am trying to obtain the Class object for this class using the following line of code

Class.forName("com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction");

When I run this code, I am getting ClassNotFoundException. We then realized that the class was not getting loaded. In order to load the class, I had to run the following lines of code:

log.warn(String.valueOf(UpdateIssueStatusFunction.class));

Class.forName("com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction");

This line loaded the class and then I was able to obtain the Class object that I needed.

Now coming to the question.

Could you please tell me how to load the class using a classLoader (or something similar) when I want to generalize the above line of code as following:

Class.forName(name); // name is a string which receives a value at runtime.

?

3 answers

1 accepted

0 votes
Accepted answer

Hello Jobin,

Thank you for your solutions. I was able to figure out the class in JIRA core API that can dynamically load classes given the name of the class as a string. The following code was able to load the class that I needed dynamically.

ClassLoaderUtils.loadClass(name, <this-class-name>.class.getClassLoader());
0 votes

Are you trying to do this in a plugin? To make the core classes available, use the following maven dependency in the pom.

<dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-core</artifactId>
            <version>${jira.version}</version>
            <scope>provided</scope>
</dependency>

Yes Jobin. I have uncommented this dependency in pom.xml.

Still not seeeing it? Make sure you clean up the project and recreate it. And then refresh the IDE.

For ecllipse:

mvn elicpse:clean eclipse:eclipse

Hello Jobin,

Thank you for your solutions and support. I have found a class that can load classes that I need given the classname. The following code was able to load the classes that I need.

 

ClassLoaderUtils.loadClass(name, this.getClass().getClassLoader())

Where name is fully qualified classname as a string, this.getClass().getClassLoader() returns the class loader of the calling class.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,798 views 18 22
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