java_method: how to use external jar?

java_method: how to use external jar?

de către Virginie Galtier-
Număr de răspunsuri: 3

Hello,

I'm using CodeRunner inside a Moodle test. I'd like my students to write a Java method that makes use of javax/json and org/glassfish libraries. I've attached the corresponding jar files in the "support files" section, and personalized the __tester__ class so it imports these packages. But it fails:

    __tester__.java:6: error: package javax.json does not exist
    import javax.json.JsonReader;

How can I fix that?

I've tried uncompressing the jar files in the "support files" section but sub-directories are deleted when I save the question.

Have a good day,

Virginie

Ca răspuns la Virginie Galtier

Re: java_method: how to use external jar?

de către Richard Lobb-

You will need to specify a classpath that includes all the jar files. My Java is very rusty and I haven't actually done this in CodeRunner, but you'll need to do something like the following:

  1. Customise the question (click the Customise checkbox).
  2. Open the Advanced customisation section.
  3. In the Sandbox parameters field, set a suitable classpath via the interpreterargs parameter, e.g. something like

     {"interpreterargs": ["-cp", "file1.jar:file2.jar:file3.jar", "-Xrs", "-Xss8m", "-Xmx200m"]}

The list of interpreterargs strings are inserted into the command line that runs the compiled class file. So the job will be compiled and run as

javac Thing.java
java -cp file1.jar:file2.jar:file3.jar -Xrs -Xss8m -Xmx200m Thing

The three -X arguments are what you get if you don't specify an explicit interpreterargs parameter.

But I'm very rusty on this - you'll probably have to experiment to get it working.

Alternatively follow the instructions for supporting or implementing new languages to define a question type that uses Python to compile and run a student's Java program. If you inspect the template for the multilanguage question type you'll be able to see the commands used to compile and run java.

I'm sorry this is a bit high level but I'm about to start the teaching year and I don't have time to test a solution for you.

This might be useful to other Java users, so would you mind posting back here when you've got it working, please?

Ca răspuns la Richard Lobb

Re: java_method: how to use external jar?

de către Virginie Galtier-

Thank you very much Richard and good luck with your teaching year!

With your help, here is what works for me:

  1. Place the "mystuff.jar" and "foo.jar" library archive files in the "Support files" section.
  2. Customise the question (click the Customise checkbox).
  3. Open the Advanced customisation section.
  4. In the Sandbox parameters field, set a suitable classpath via the compileargs and interpreterargs parameters as follow (I took the -X parameters values from moodle-qtype_coderunner-master/db/builtin_PROTOTYPES.xml):

     {"compileargs": ["-cp", "mystuff.jar:foo.jar", "-J-Xss64m", "-J-Xmx4g"], "interpreterargs": ["-cp", ".:mystuff.jar:foo.jar", "-Xss16m", "-Xmx500m"]}

Ca răspuns la Virginie Galtier

Re: java_method: how to use external jar?

de către Richard Lobb-

Good to know it's working (with compileargs added as well). Thanks for posting, Virginie.

Richard