How to load a jira core api class dynamically?

Usman M August 26, 2014

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
Answer accepted
Usman M September 9, 2014

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
Usman M September 14, 2014

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.

0 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 26, 2014

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>

Usman M August 26, 2014
Yes Jobin. I have uncommented this dependency in pom.xml.
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 26, 2014

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

Suggest an answer

Log in or Sign up to answer