Call a static global function

Hi,

I’ve created a class with a static méthod : helloWorld()

How to call helloWorld from all my others class and packages ?
My import clause fails to compile… and the call of my function too…

Out of webMethods I do not meet the problem…

Regards

Hi Cedric,

all Packages which need access to your helloWorld-methods must declare a dependency to the package in which the method is located.

Usually each package uses is own class loading context.
But declaring the dependency forces the class loader to load all packages affected in the lowest common class loader context.

Can you share some error messages as well as some more details about your use case, please?

Regards,
Holger

In my previous expereince if we had to access bunch of methods throughout all the Packages on Integration server, we would package all the methods in to single class and then export the Java project from Eclipse as a Jar. We place this jar in the IS\packagename\code\jars\static folder. Not sure about 9.x or higher, you can try it.

JOUBERT – I didn’t understand your question, can you please elaborate.

Thanks,

Thanks for your replies. I would hop to avoid package my class and functions in an external Jar… but why not…

To try to elaborate,

I have a public class :


package mypackage.utils.tests;

public final class myTest_SVC
{

   public static final void myTest(IData pipeline) throws ServiceException {
   }
   public static void helloWorld(String text)
   {
   ...
   }
   final static myTest_SVC _instance = new myTest_SVC();

   static myTest_SVC _newInstance() { return new myTest_SVC(); }

   static myTest_SVC _cast(Object o) { return (myTest_SVC)o; }
}

Now, from another class of package (in another directory), I would like to access helloWorld() function :


package mypackage.utils.calls;
import mypackage.utils.tests.myTest_SVC;

public final class myCall_SVC
{
   public static final void myCall(IData pipeline) throws ServiceException {
      // ... calling myTest_SVC.helloWorld(...);
   }
}

From the Import clause, I meet this error :

The source was saved, but was not compiled due to the following errors:
error: cannot find symbol
import mypackage.utils.tests.myTest_SVC;
…^
symbol: class myTest_SVC
location: class tests
1 error

Any idea ?

Regards

Hi Akshith,

this is still possible.

But as long as the jar does not need to be loaded in main IS classpath it can be placed under code/jars/, where reloading the package is sufficient when jar changes.

When the jar is located under code/jars/static, it is neccessary to shutdown and start the IS to get the changed jar loaded.

Regards,
Holger

Thanks for the confirmation Holger!

As for the error that he is reporting, he might have to add the jar to the Designer Project\lib folder as well for Eclipse to recognize the import?

I thouht there is no need to add the Jar in the Project\lib, because the Eclipse completion added the import clause automatically in my myCall_SVC class.

Thanks for your help guys :slight_smile:

Hi,

eventually reloading the package where the static function is located might help to make sure that the class has been loaded.

Adding the jar in Designer is not neccessary to get the source compiled on remote IS.

Regards,
Holger

Thanks for your help.

Regards

Hi,

I’ve made a test with an external JAR and all works well.

But I have another need please, but I do not want to deal with another external JAR.

I need to call a Java function (or java class) from anywhere… regardless the package in which the class is definied. (like service.invoke() does to access a service Flow…)

  • call a Java function inside the same package

    PACKAGE1.package1.classFolder1
    PACKAGE1.package1.classFolder2
    PACKAGE1.package1.classFolder3

    I would like to use a the function defined in classFolder1, and access it from classFolder2 and classFolder3

  • call a Java function from another package

    PACKAGE1.package1.classFolder1
    PACKAGE2.package2.classFolder1

    I would like to use a the function defined in the first packaqe, and access it from another package ?

I suppose what I need is possible to do ?

Hi Cedric,

add a Package Depency in the propeties of package 2 pointing to package 1 and reload package 1.
This causes both packages to be reloaded.

After that you should be able to consume the java service from package 1 in package 2.

Regards,
Holger

Thanks for your reply.
What do you mean by “consume”… calling service.invoke() ?

I’ve tried to add my depencies, but after reloading the 2 packages,
I can’t import Packaqe1 from Package2, neither import Package2 from Package1.

Am I missing something ?

Regards

Hi Cedric,

just add the import statements in the java service development for the classes (either IS java service or classes in external jar).

Can you share the import statements?

Regards,
Holger

Hi Holger,

In this thread, I’ve first had the need to share classes using an external Jar. I made what you explained me, all is working fine.

Today, I have a new need : I do not want to use any external Jar.
I’m trying to call a Java class method from packages both created in webMethods : Package1 and Package2.
When I try to import the package2 in the class of Package1, the Package2 is not visible, I have the error :

package package1.utils;
import package2.* —> “error: package package2 does not exist”

After adding the dependencies in the Package option, I thought that my import would work, but no.
I do not understand.

Hi Cedric,

the package name ist not part of the class name.

Check the code/source and code/classes directories in your package on the file system.

The generated and compiled java code is stored there.

Regards,
Holger

Hi Holger,

It’s ok for invoking the service package2 to consume from package1.

But, calling a class method or a method in Package2, from a class in Package1 do not still working…
Am I missing something ?