1

I have the programming game 'Robocode' in my project root so I can run the project anywhere and not have it installed in C:/robocode. I have a run configuration with the following options;

  • Main Class: robocode.Robocode
  • VM Options: -Xmx512M -Dsun.io.useCanonCaches=false -Ddebug=true -DNOSECURITY=true -cp libs/robocode.jar
  • Working directory: MY_ROOT\robocode_master
  • JRE: Default(9.0.1 - SDK of my main module)

I'm writing some custom functionality for an AdvancedRobot that connects to a MongoDBAtlas Cluster for necessary data. I'm using mongo-java-driver-3.6.1. The .jar has been added as a library to IntelliJ, then as a compile dependency on the main module. It works fine in testing, I can download documents etc.

To illustrate, the libraries are shown here in the Project structure pane.

Project Structure

I have set the compile output path to the robocode_master/robots directory so that when I run Robocode from IntelliJ, it will see the custom robot .class files in the correct directory and allow me to add them to a battle.

The problem is when I start the battle and the robot tries to do what it needs to do, it throws a java.io.FileNotFoundException and is trying to find the necessary library files from the robocode_master/robots directory.

For example, the first thing it does is connect to the DB;

java.io.FileNotFoundException: MY_ROOT\robocode_master\robots\com\mongodb\MongoClient.class (The system cannot find the path specified)

When I have my project output paths set to the robots folder, upon launching Robocode, it throws a bunch of FileNotFoundExceptions. But if I switch the options to 'Inherit project compile output path', then launch Robocode, I don't get the exceptions upon launch. But now of course, my robots aren't in the default directory, so it can't see them. So I add the new path in out/production/... to Robocode in the GUI and reboot, and I then get the same exceptions from the robots. It now tries to look for the files in out/production/.../com/mongodb/MongoClient.class

How can I 'tell' Robocode to look for the libraries in their default location on the classpath?

TomPlum
  • 135
  • 1
  • 14
  • I am not at home right now and don't think I can really help with that (I have had similar classpath problems with Robocode in the past and I think there might be some problem with its handling). But have you tried adding the correct path to the VM options' `-cp` argument? – InvisiblePanda Mar 08 '18 at 06:41
  • I’ve added the correct path to the robocode.jar with the -cp argument. So the game launches successfully, it’s the paths to my module libraries that cannot be found. – TomPlum Mar 08 '18 at 08:30
  • Oh, I meant adding the path to your module libraries to the `-cp` (semicolon-separated after the `robocode.jar`). But it might not work (it "kind of" worked for me and my problem, but I had different errors back then). – InvisiblePanda Mar 08 '18 at 12:19
  • Ah, I understand. Would I add that to the VM Options in the Application Run Configuration (That starts Robocode) ? – TomPlum Mar 08 '18 at 12:37
  • Yes, in your run configuration (the **"VM Options""* line at the top of your post). But as I said: don't get your hopes up. Someone else might know more though, I'm not much of a Java developer. AFAIK Robocode has a self-built class loader that might work in mysterious ways. It's Open Source, so you could always have a look at the Robocode source to find out more. – InvisiblePanda Mar 08 '18 at 13:24
  • Semicolon separating paths doesn't seem to work. I've tried moving all the relevant .jar files to the working directory in the **robocode_master** and adding their paths to the `-cp` argument. But the IntelliJ throws `Error: Could not find or load main class libs.robocode.jar Caused by: java.lang.ClassNotFoundException: libs.robocode.jar`, for example. – TomPlum Mar 08 '18 at 13:45
  • Why is it `libs.robocode.jar` instead of a / or \ between `libs` and `robocode.jar`? – InvisiblePanda Mar 09 '18 at 07:41
  • Unsure, the paths in the VMOptions have forward slashes, any path I use ends up with the . separating it in the console output. I renamed all the jars to single word, lowercase. `libs/mongo.jar` but the console then cannot find `libs.mongo.jar` – TomPlum Mar 09 '18 at 08:03

1 Answers1

0

Robocode will look for robot classes in your /robots folder. If your robot is located somewhere else, you need to tell Robocode where to locate these from the Robocode UI by setting with the Developments Options. Robocode has it's own class loader for loading Robot classes - but only robot classes.

Other classes/libraries must be specified on the ordinary classpath for Robocode using the -cp or -classpath for the java command (VM Options) like done for any other Java application.

Also notice, that you should use the VM Options that comes with Robocode - like the ones specified in the robocode.bat file. Especially when you use Java 9 or newer:

java -Xmx512M -cp libs/robocode.jar -XX:+IgnoreUnrecognizedVMOptions "--add-opens=java.base/sun.net.www.protocol.jar=ALL-UNNAMED" "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED" "--add-opens=java.desktop/sun.awt=ALL-UNNAMED" robocode.Robocode %*

You could copy this into "run_my_stuff.bat" and add stuff to the -cp right after libs/robocode.jar.

However, I am not sure if it will work with the Mongo driver. Robocode was built for simple robots with limited access to resources like CPU cycles, file access etc.

FNL
  • 133
  • 1
  • 8
  • As I mentioned in my post, I have set the output folder to the `/robots` folder. The robots are loaded successfully, they just cannot invoke any methods from class in the libraries due to the FileNotFound Exceptions. I'm trying a new approach using the package control method. Now I'm getting an `invalid robot or team` error when loading my own robots. I've posted [here](https://groups.google.com/forum/#!topic/robocode/srza4x8R7oE) on the Google group. – TomPlum Mar 24 '18 at 21:07