Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Run Java Class on Bundle Start

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

04-07-2018

Hi,

Is there a way to run a java class on a bundle start up like a main class?

Replies

Avatar

Avatar
Give Back 3
Level 2
indra_kumarg290
Level 2

Likes

4 likes

Total Posts

6 posts

Correct Reply

1 solution
Top badges earned
Give Back 3
Give Back
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Give Back 3
Level 2
indra_kumarg290
Level 2

Likes

4 likes

Total Posts

6 posts

Correct Reply

1 solution
Top badges earned
Give Back 3
Give Back
Boost 3
Boost 1
Affirm 1
View profile
indra_kumarg290
Level 2

04-07-2018

You can put an OSGi service component inside the bundle and in activate method of the service component you can run your java class.

e.g.

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component

@Service

public class MyApplicationStarterService{

  @Activate
  protected void activate(ComponentContext ctx) {

      MyAppClass app = new MyAppClass();

      app.run();

  }

}

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

05-07-2018

This isn't exactly answering the question, because the activation of this component is lazy, and thus is asynchronous to the activation of a bundle.

Instead you should specify a BundleActivator in the metadata of your OSGI bundle. The code of this class is executed during the bundle start (also meaning, that an unhandled exception in the BundleActivator prevents the bundle from starting.

The definition in the POM: acs-aem-commons/pom.xml at master · Adobe-Consulting-Services/acs-aem-commons · GitHub

The BundleActivator class: acs-aem-commons/Activator.java at master · Adobe-Consulting-Services/acs-aem-commons · GitHub

regards,

Jörg