Hi all, beginner question here.
I have a file in my trans folder with some custom java functions that I would like to re-use in other modules. Lets call this file: “trans/CoolFunctions.java”
I can’t manage to import that file into other java files. Can anyone spare a hand?
Let’s say I have a new file: “trans/SecondFile.java”. How do I import all functions from “trans/CoolFunctions.java” into it?
None of the below works:
import CoolFunctions;
import CollFunctions.java;
import trans.CoolFunctions;
import trans.CoolFunctions.java;
import trans/CoolFunctions;
import trans/CoolFunctions.java;
Any ideas?
Thanks.
Hi rodrigofrb ,
Thank you for your inquiry. To better understand your use-case, I’ve prepared a list of questions below that I would appreciate your responses to. I look forward to hearing from you.
List of questions:
__________________
1.) Can you kindly elaborate on exactly what you mean by you can’t import that file (namely, trans/CoolFunctions.java) into other java files? Are you receiving some sort of error when trying to import it?
2.) In the code example you attached, where is the code from? From what clover component does the code originate?
3.) In the cool functions file, what is the name of the class in that file?
4.) In the second line of the code example, did you make a typo in “import CollFunctions.java”? I presume you meant “import CoolFunctions.java” instead.
5.) What version of CloverDX Designer are you running?
6.) If it’s not too much of a hassle, can you send us the graph and the java file you’re working with? It’ll help us better understand your use-case.
Best Regards,
George Darvehn
Hi George,
thanks for the interest. See below the answer to your queries.
1.) Can you kindly elaborate on exactly what you mean by you can’t import that file (namely, trans/CoolFunctions.java) into other java files? Are you receiving some sort of error when trying to import it?
Answer: Yes, I get a java syntax error: “The import CoolFunction cannot be resolved.”
2.) In the code example you attached, where is the code from? From what clover component does the code originate?
Answer: The code is in a java file, saved in the trans folder. I am then assigning this java file to a CustomJavaTransformer component.
3.) In the cool functions file, what is the name of the class in that file?
Answer: public class CoolFunctions extends AbstractGenericTransform {
4.) In the second line of the code example, did you make a typo in “import CollFunctions.java”? I presume you meant “import
CoolFunctions.java” instead.
Answer: Yes, that was a typo.
5.) What version of CloverDX Designer are you running?
Answer: 4.8.2
6.) If it’s not too much of a hassle, can you send us the graph and the java file you’re working with? It’ll help us better understand your use-case.
Answer: No problem. I’ve recreated the problem in its simplest form and attached as requested.
Hi rodrigofrb,
I have reviewed your test project and here are my findings.
The reason why you weren’t able to import the CoolFunctions class is that in Java you cannot import types from an unnamed (default) package. This is the case when you put your *.java files directly into the trans folder. So, in order to be able to import it, your java file has to be in a subfolder of the trans folder and contain a package statement. The package name should ideally respect the Java package naming conventions.
Although this was the main issue, I’ve found more, whereas some had the potential to confuse you but some could render you unable to run your graph(s).
1. Component properties Algorithm, Algorithm URL and Algorithm class are mutually exclusive. Therefore, when you configure more than one of these, there wouldn’t be any error displayed but only the one with the highest priority will be used. Their priorities are the same as their order in the component edit dialog. So Algorithm has the highest and Algorithm class the lowest priority. That implies there’s always only one class that is used as the “core” of the CustomJava component. This behavior might be useful for some complex cases but this doesn’t seem to be one of them.
2. Your CoolFunctions class doesn’t need to extend AbstractGenericTransform class. Only the class that serves as the “core” class of the CustomJava component needs to do this - Transform class in your case.
3. In Java you don’t import files but types (a class in this case). So the correct import statement should look something like import com.companyname.main.CoolFunctions; instead of import com.companyname.main.CoolFunctions.java;.
4. I noticed you tried to use the myCoolFunction() method without instantiating the class it is a member of and referencing the method using the instance name. I’m not sure if this is because you created the example in a hurry but that wouldn’t work. Alternatively you can make the method static and then reference it using the class name, i.e. CoolFunctions.myCoolFunction();.
I hope I didn’t forget anything but this should get you going. Attached is your project, modified to be working so you can compare them.
Best regards.
Thanks Lukas, that’s brilliant!
It is working fine now.