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

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,009 views 12 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot