0

I want to invoke few java methods from an Airflow task(s) since I already have lots of code written in java including validation, cleansing, some business logic and I don't want to rewrite that in Python again.

I am using Airflow 2.1.0

I know there is a way to invoke a jar using BashOperator

jar_task = BashOperator(
    task_id='java',
    bash_command='java -jar somejar.jar',
    dag=dag
) 

But is there any way using which I can call any specific java class or method directly from the task itself, otherwise I have to create many small jars for each of my logic.
Thanks in advance.

Shashank Gupta
  • 321
  • 3
  • 15
  • why don't you convert your java application into a java spring boot application so that you can call the end point? – Praneeth Kumar Jun 15 '21 at 07:59
  • @PraneethKumar yes that is also possible, but for that I need to create many rest end points. I was hoping for a direct solution if available in Airflow. If not, I can try this – Shashank Gupta Jun 15 '21 at 08:10
  • Did you try reading the [documentation](https://docs.oracle.com/en/java/javase/20/docs/specs/man/java.html) for the `java` command? – Abra Aug 26 '23 at 07:14

2 Answers2

0

You could use java -cp somejar.jar SomeClass to run a different main method.

roby
  • 3,103
  • 1
  • 16
  • 14
0
java_command = 'java -cp .:{lib_path}/* class_name args'
java_task = BashOperator(
    task_id="java_task",
    bash_command=java_command,
)

lib_path=Path to directory where your required jar file is present class_name = com.x.y.ClassName args = Argument should be in space seperated if your class is expecting more than one arguments