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

This widget could not be displayed.

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());
This widget could not be displayed.

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

This widget could not be displayed.

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
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

234 views 1 3
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