So it looks like robots can't even read from their own JAR files?

764 views
Skip to first unread message

da...@code.davidpcaldwell.com

unread,
Oct 29, 2013, 9:26:20 PM10/29/13
to robo...@googlegroups.com
I am attempting to load a configuration file from within my own JAR file using Class.getResourceAsStream() and it is failing with:

Preventing inonit.robocode.script.Robot 0.0.1 from access: ("java.io.FilePermission" "/path/to/robocode/robots/inonit.robocode.script.Robot_0.0.1.jar" "read"). You may only read files in your own root package directory.

I have attempted to place the file both in the top level of the JAR file and also as a sibling of my main robot class (in inonit/robocode/script/).

Is this by design? Am I doing something wrong?

-- David P. Caldwell

fnl

unread,
Oct 30, 2013, 5:45:02 PM10/30/13
to robo...@googlegroups.com
Hi David,

Robots are not allowed to use standard Java I/O APIs by design. Robots are run in a restricted sandbox inside Robocode due to various security reasons to protect the end-user's system, but also to limit the how much resources a robot can use as a part of the game.
Note that it is possible to disable some of the security mechanism by using the -DNOSECURITY option. But this is only valid on the local system.

An AdvancedRobot is only able to access it's own private directory by using getDataDirectory() and get a file by using getDataFile(). You can write to a file using RobocodeFileOutputStream or RobocodeFileWriter, which works the same way as the standard FileOutputStream and FileWriter, like you might be used to.

Note that whenever Robocode loads a robot's .jar file it is automatically extracted into it's own private directory inside [robocode dir]/robots, so that would be something like [robocode dir]/robots/inonit/robocode/script/Robot for your robot.
All files located in your .jar file are extracted to this directory including .class and .properties files, but also the files you have included with your .jar file. So you are able to read your file by using getDataFile("name of your file"). Notice that the root directory is returned by getDataDirectory().

I hope this helps?

Cheers,
- Flemming

cen...@davidpcaldwell.com

unread,
Nov 1, 2013, 7:36:35 AM11/1/13
to robo...@googlegroups.com
On Wednesday, October 30, 2013 5:45:02 PM UTC-4, fnl wrote:
Note that whenever Robocode loads a robot's .jar file it is automatically extracted into it's own private directory inside [robocode dir]/robots, so that would be something like [robocode dir]/robots/inonit/robocode/script/Robot for your robot.
All files located in your .jar file are extracted to this directory including .class and .properties files, but also the files you have included with your .jar file. So you are able to read your file by using getDataFile("name of your file"). Notice that the root directory is returned by getDataDirectory().

This does not seem to be true. I'm running on Mac OS X. Running a robot *does* seem to result in a subdirectory for that robot being created under $ROBOCODE_HOME/robots/.data, which seems to be for writing scratch data (SittingDuck uses it). And I assume *this* is the directory returned by getDataDirectory(). But the JAR files are not extracted, and hence bundled property files and other files are not extracted.

Patrick Cupka

unread,
Nov 1, 2013, 12:52:56 PM11/1/13
to robo...@googlegroups.com, robo...@googlegroups.com
I'm not at a Robocode install right now to verify anything, but if you want to package a file in your JAR that your robot can read at runtime, it needs to be in the .data dir. Robocode will package this dir if it exists, or at least used to. You can't read the .properties file that Robocode creates for your bot, for instance, which is in the parent dir of .data.

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Patrick Cupka

unread,
Nov 1, 2013, 1:00:42 PM11/1/13
to robo...@googlegroups.com
Here's a JAR of an old bot of mine where I preload data. I think it's the "data" subdirectory under the dir of your main robot class, but you can double check how that JAR is structured. (Sorry I can't right now...)

fnl

unread,
Nov 1, 2013, 5:28:03 PM11/1/13
to
I made a test myself by writting a simple robot, which I have packaged into a robot .jar file:


This robot reads a test.txt file and writes the content out to the robot console. The test.txt file exists under the example/DataBot.data dir.
On the local system, robot data is located under [robocode dir]/robot/.data/[robot package]. This folder allows robots to read and write to their own files using "standard" I/O, which is not necessarily possible to do directly on the .jar file for the robot, which could be locked, read-only etc. 
When the robot is packaged using the Robot Packager, the .jar file should include a similar dir as XYZ.data, where XYZ is the name of the robot.

However, the Robot Packager in Robocode 1.8.3.0 (the newest version of Robocode when writing this) is broken so it does not include the data for the robot.
It used to work in older version of Robocode. I have created a bug report here so this issue can be solved:


Currently, the work-around is to either create a XYZ.data dir inside your .jar file containing the files you need to read. You could also copy the [robocode dir]/robot/.data/[robot package] into your XYZ.data dir in the .jar file.

Have a look at the /example/DataBot.java source file my .jar file above. It shows you how to read out the content from the file included in the .jar file. Also have a look at the folder structure in the .jar file. :-)

I hope this helps?

Cheers,
- Flemming


da...@code.davidpcaldwell.com

unread,
Nov 1, 2013, 6:20:40 PM11/1/13
to robo...@googlegroups.com
On Friday, November 1, 2013 12:52:56 PM UTC-4, Patrick Cupka wrote:
I'm not at a Robocode install right now to verify anything, but if you want to package a file in your JAR that your robot can read at runtime, it needs to be in the .data dir. Robocode will package this dir if it exists, or at least used to. You can't read the .properties file that Robocode creates for your bot, for instance, which is in the parent dir of .data.

Now I get it. I saw my data directory being created but was not aware there was a special path in my JAR file that I could use to prepopulate it.

For now I've implemented an absurd workaround in which I preprocess my data files into a Java source file that creates a HashMap containing the data. Despite the fact that my code to do this is so beautiful :), it will be a joy to rip it out and simplify the process.

Thanks for the clarity, Patrick and Flemming.

da...@code.davidpcaldwell.com

unread,
Nov 2, 2013, 5:25:28 AM11/2/13
to robo...@googlegroups.com
On Friday, November 1, 2013 6:20:40 PM UTC-4, da...@code.davidpcaldwell.com wrote:
For now I've implemented an absurd workaround in which I preprocess my data files into a Java source file that creates a HashMap containing the data. Despite the fact that my code to do this is so beautiful :), it will be a joy to rip it out and simplify the process.

I have it working now. The one thing I ran into that I didn't expect is that the robot's "data area" (under the appropriate JAR path) cannot, at least on initialization, itself contain folders. If it does, at least under 1.8.3.0, the attempt to create it blows up when the engine attempts to copy the contents of the directory.

This could also (it occurs to me; I didn't look at the code) have to do with the fact that I am packaging myself and am, as most ZIP tools do, creating path entries in the ZIP file:

MyFolder/
MyFolder/MyFile.properties

It is possible the robot packager works differently and would have allowed me to have MyFolder/MyFile.properties if I did not have the folder entries.

fnl

unread,
Nov 4, 2013, 2:56:25 PM11/4/13
to robo...@googlegroups.com
Hi David,

I am happy to hear that you got it working now.
Regarding the support for folders, I don't think Robocode supports folders inside the robot data directory, but it should be a little effort to add support for it.
So I will try to implement this for the next version of Robocode (version 1.9.0.0) while fixing the issue with the Robot Packager, which does not include the files from the .data directory for the robot, when packaging it. :-)

Thanks and regards,
- Flemming
Reply all
Reply to author
Forward
0 new messages