Hello,
I'm having trouble getting the following to work.
I have a java_class question for students to develop. My sample answer looks like this:
package foobar;
public class Foo {
private Bar bar;
public String toString() {return "whatever";}
}
The class Foo is in a support file called Bar.java containing:
package foobar;
public class Bar {}
Not very exciting I'll admit.
In the question preview with the above sample answer submitted as the student answer, Check gives:
Debug: source code from all test runs
Run 1
package foobar; class Foo { private Bar bar; public String toString() { return "whatever"; } } public class __Tester__ { public static void main(String[] args) { __Tester__ main = new __Tester__(); main.runTests(); } public void runTests() { System.out.println(new Foo()); } }
which doesn't look unreasonable. However, the compiler seems unable to find the class Bar even though it's declared in the same package.
Syntax Error(s)
__Tester__.java:4: error: cannot find symbol private Bar bar; ^ symbol: class Bar location: class Foo 1 error
Take out the package declarations and the code works as expected. I've tried diddling the template, eg. putting a package declaration at the top, with no great success.
Take out the variable declaration Bar bar and removing the support file makes the class compile, but Check now gives a runtime error:
Test | Expected | Got | ||
---|---|---|---|---|
System.out.println(new Foo()) | whatever | ***Runtime error*** Exception in thread "main" java.lang.NoClassDefFoundError: __Tester__ (wrong name: foobar/__Tester__) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) |
Has anyone gotten questions with Java classes in packages to work?
I should mention that I can strip out the package declaration from the student answer with
{{ STUDENT_ANSWER | replace({'package foobar;': ''}) | replace({'public class ': 'class ' }) }}
in the template, but that just seems awkward.