Universal Robots+

Loading Script File from the Resources Folder

I am trying to load a urscript file from the resources folder and execute it in the Installation node (similar to the ScriptWrapper Urcap example), but the file cannot be found from the Java code.
I saw @recognitionrobotics was having a similar issue, but with no resolution.

I placed the script file in the resources folder. Executing “jar tf” on the final jar file verifies the file is where it supposed to be, however my code cannot find it:

String filePath = getClass().getClassLoader().getResource("/com/impl/" + FILE).getFile();
File file = new File(filePath);

BTW, Accessing png images using a similar method does work:

URL iconPath = getClass().getResource("/com/impl/logos/CheckV.png");
lblCheck.setImage(ImageIO.read(iconPath));

Help would be appreciated.
Omer.

Maybe try accessing the text file by using a StreamReader. The object type “File” is usually used for files on the local file system, but the text file actually exists inside the .urcap archive, that is basically a .jar archive.

E.g. something like this (I’m sure there are many StreamReader examples out there):
java.net.URL fileURL = getClass().getResource("code.txt");
InputStreamReader inputReader = new InputStreamReader(getClass().getResourceAsStream(fileURL.getPath()));
BufferedReader reader = new BufferedReader(inputReader);

After that you can e.g. read in the individual lines of the text file, use a StringBuilder etc.
String line = reader.readLine();

2 Likes

Your code worker perfectly, @artiminds, Thank you.

But im stuck on writing to that file. could you share a snip of code to make it?