ekka
ekka

Reputation: 355

calling methods in a jar file from php

I am running a jar file from the php function.

exec("java -jar hel.jar", $outputArray);

But i want to call a particular method in the jar file and send parameters to those methods and get the final output.

Example:-

public class hel {

    public static void main(String[] args) {
         hel he=new hel();
        he.getName("john");
    }

   public String getName(String name){
        System.out.println(name);
        return name;

    }

}

I want to send a String value as parameter to method "getName" and get the final output. Any suggestions?

Upvotes: 0

Views: 1833

Answers (1)

Varis
Varis

Reputation: 254

You can only call the main method from the main class. From the java manual:

-jar filename

Executes a program encapsulated in a JAR file. The filename argument is the name of a JAR file with a manifest that contains a line in the form Main-Class:classname that defines the class with the public static void main(String[] args) method that serves as your application's starting point.

So for it to work you have to setup your manifest in the jar and write your main class to call the function you want. When that's done, append any arguments to the exec call and it should work.

You have to create a manifest file "MANIFEST.MF" in the "META-INF" folder of the jar with these contents:

Manifest-Version: 1.0

Main-Class: This.Is.Your.Package.hel

and you can add arguments to exec like this:

exec("java -jar hel.jar first_argument second_argument", $outputArray);

add quotes if your arguments need spaces. Your arguments will show up in the String[] args array.

Upvotes: 2

Related Questions