# Importing Functions from a second Java file

**URL:** <https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060>\
**Category:** CloverDX Platform\
**Created:** [November 22, 2019, 12:00am UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060 "2019-11-22T00:00:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigofrb](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@rodrigofrb](https://forum.cloverdx.com/u/rodrigofrb)\
**Post date:** [November 22, 2019, 12:00am UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060/1 "2019-11-22T00:00:00Z")

</div>

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:

```auto

import CoolFunctions;
import CollFunctions.java;
import trans.CoolFunctions;
import trans.CoolFunctions.java;
import trans/CoolFunctions;
import trans/CoolFunctions.java;

```

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![darvehng](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@darvehng](https://forum.cloverdx.com/u/darvehng)\
**Post date:** [November 27, 2019, 3:00pm UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060/2 "2019-11-27T15:00:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rodrigofrb](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@rodrigofrb](https://forum.cloverdx.com/u/rodrigofrb)\
**Post date:** [November 29, 2019, 5:04pm UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060/3 "2019-11-29T17:04:19Z")

</div>

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.

- [testGraph.zip](https://cdn.cloverdx.com/forum/19016%5F1c55f5b3ed5ea507be82bacca453864b/testGraph.zip)

---

<div class="post-metadata">

**Author:** ![Lukas\_Cholasta](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.cloverdx.com/lukas_cholasta/32/166_2.png) [@Lukas\_Cholasta](https://forum.cloverdx.com/u/Lukas_Cholasta)\
**Post date:** [December 3, 2019, 2:31pm UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060/4 "2019-12-03T14:31:12Z")

</div>

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](https://stackoverflow.com/questions/33645950/import-package-with-no-name-java). 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](https://docs.oracle.com/javase/tutorial/java/package/createpkgs.html). The package name should ideally respect the Java [package naming conventions](https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html).

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](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html) the class it is a member of and [referencing the method using the instance name](https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html). 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](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html)_ 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.

- [JavaImport.zip](https://cdn.cloverdx.com/forum/18108%5F9f158c9c8cf70216d5fddb790e147262/JavaImport.zip)

---

<div class="post-metadata">

**Author:** ![rodrigofrb](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@rodrigofrb](https://forum.cloverdx.com/u/rodrigofrb)\
**Post date:** [December 3, 2019, 3:39pm UTC](https://forum.cloverdx.com/t/importing-functions-from-a-second-java-file/2060/5 "2019-12-03T15:39:41Z")

</div>

Thanks Lukas, that’s brilliant!

It is working fine now.
