Java packages

Java packages

by sc kaindorf -
Number of replies: 5

Oh dear - I'm so frustrated - I cant find the error. My wife asked me the whole time, what I do all the time for our school ... Hhhmmm :-)

What I have done is:

* new Prototype for java packages

import sys, os, shutil, subprocess
__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""
# Make package directory, put support files into it
os.mkdir("{{ QUESTION.parameters.package }}")
[shutil.move(f, "{{ QUESTION.parameters.package }}") for f in os.listdir() if f.endswith(".java")]
# Put student answer class into package directory
with open("{{ QUESTION.parameters.package }}/{{ QUESTION.parameters.class}}.java", "w") as f:
    print(__student_answer__, file=f)
    
# Build test class in package directory too
tester = """package {{ QUESTION.parameters.package }};
public class __Tester__ {
    public static void main(String[] args) {
        {{TEST.testcode}}
    }
}
"""
with open("{{ QUESTION.parameters.package }}/__Tester__.java", "w") as f:
    print(tester, file=f)
# Compile and run
if (subprocess.call(["javac", "{{ QUESTION.parameters.package }}/__Tester__.java"]) != 0 or
   subprocess.call(["java", "{{ QUESTION.parameters.package }}/__Tester__"]) != 0):
    print("Error")
    print("** Further testing aborted **", file=sys.stderr)
print("Well Done?")

* I have two java files: one for the answer and an additional

* Parameter set to {"package": "Bsp206", "class": "IqTester"}

* My testcase is simple:

System.out.println("DEBUG0");
IqTester iqt = new IqTester();
iqt.start();


But: Nothing happens! No output - nothing! Could anyone please help!

Thanks, thanks, thanks

PS (On my ubuntu laptop javac and java works fine ...)


In reply to sc kaindorf

Re: Java packages

by Richard Lobb -

It is strange to get no output at all.

Have you set memlimit to 0 so that the sandbox doesn't limit memory at all? This is pretty safe with Java because the JVM manages memory anyway.

Have you turned on template debugging, copied the program that's generated out to your own machine and run it with the support file in the same directory as the Python expanded template? Does it work?

If you're still stumped, please post back attaching the exported xml question together with the Java file(s) that you're testing with and I'll have a look at it.

Richard

In reply to Richard Lobb

Re: Java packages

by sc kaindorf -

Thanks Richard for your time and coderunner ...

Find attached the export and the source files.

In reply to sc kaindorf

Re: Java packages

by Richard Lobb -

Your exported question needs your Java_Package prototype, which I don't have so I can't test exactly. However, I changed the question type to python3, customised it, set the template to the code you gave above, and set the memlimit to 0 and numprocs to 100. When I run it get the output "Well Done?" from your template:

Can you confirm please that you have tried setting memlimit to 0 and that numprocs has been set to a higher number (both under Advanced Customisation) ?

Result table image

I attach my working version (with the "Expected" set to "Well Done?"). Note that I also checked Hoist Template Parameters so I could dispense with all the QUESTION.parameters prefixes in your template code.

[Edit: it belatedly occurs to me that because I exported the question from my development system, which has extra database fields, the attached .xml file might not be importable onto your system. I can redo the exercise on a production system if necessary, but please try it first, after confirming the memlimit and numprocs values.]

By the way: I think it would be easier you you to debug template code in a single customised question first, and only set up a prototype once that sample question is working.


In reply to Richard Lobb

Re: Java packages

by sc kaindorf -

Thanks for your answer and "Frohes Fest" (Happy Christmas)! I have build a new Question for my students and modified the template. Its still the same, but added some print commands for a better debugging.

May I found probably my error. I use the python script found in the forum:


import sys, os, shutil, subprocess __student_answer__ = """{{ STUDENT_ANSWER | e('py') }}""" # Make package directory, put support files into it os.mkdir("{{ QUESTION.parameters.package }}") [shutil.move(f, "{{ QUESTION.parameters.package }}") for f in os.listdir() if f.endswith(".java")] # Put student answer class into package directory with open("{{ QUESTION.parameters.package }}/{{ QUESTION.parameters.class}}.java", "w") as f: print(__student_answer__, file=f) # Build test class in package directory too print('Testcode : {{ TEST.testcode }}') tester = """package {{ QUESTION.parameters.package }}; public class __Tester__ { public static void main(String[] args) { //Hello //Test: {{ TEST.testcode }} } } """ with open("{{ QUESTION.parameters.package }}/__Tester__.java", "w") as f: print(tester, file=f) # Compile and run if (subprocess.call(["javac", "{{ QUESTION.parameters.package }}/__Tester__.java"]) != 0 or subprocess.call(["java", "{{ QUESTION.parameters.package }}/__Tester__"]) != 0): print("** Further testing aborted **", file=sys.stderr)

and in my python environment everything works fine. The problem seams to be the {{ TEST.testcode }} for the generated __Tester__ class. {{ TEST.testcode }} is empty and I have no idea to fix this circumstance. The debug message from my student account is:

import sys, os, shutil, subprocess
__student_answer__ = """"""
# Make package directory, put support files into it
os.mkdir("Bsp211")
[shutil.move(f, "Bsp211") for f in os.listdir() if f.endswith(".java")]
# Put student answer class into package directory
with open("Bsp211/Bsp211.java", "w") as f:
    print(__student_answer__, file=f)

# Build test class in package directory too
print('Testcode : ')
tester = """package Bsp211;
public class __Tester__ {
    public static void main(String[] args) {
        //Hello
        //Test: 
    }
}
"""
with open("Bsp211/__Tester__.java", "w") as f:
    print(tester, file=f)
# Compile and run
if (subprocess.call(["javac", "Bsp211/__Tester__.java"]) != 0 or
   subprocess.call(["java", "Bsp211/__Tester__"]) != 0):
    print("** Further testing aborted **", file=sys.stderr)

and the added test should be:

NotenUi ui = new NotenUi();
ui.start();

Do you have another hint for me? Attached my export. Meanwhile: Here in Austria we have a lot of snow and a hard lockdown :-) - Hope you are fine and on this way: Happy New Year ... Thanks - schtl

In reply to sc kaindorf

Re: Java packages

by Richard Lobb -

Happy Christmas to you too :-) My sympathies for your heavy lockdown over Christmas. We're fortunate in New Zealand that we're a small island nation at the bottom of the world with a good government that has managed to eliminate covid, although there could be an outbreak at any time.

Without your prototype I can't check exactly what you're doing but I'm almost certain that the problem is that the "Is combinator" checkbox is checked in your prototype. Since the template is a per-test template not a combinator template, this will result in the TEST variable being undefined. You should uncheck it.

I attach an exported version of your question that uses your template within a customised question rather than as a question prototype. In its present form it's not usable because your support module generates random data so the output is non-deterministic, but it does at least run and generate plausible output. I suggest you steer clear of using randomisation until you have more familiarity with CodeRunner and its randomisation capabilities.