Menu

#853 Can't Invoke main Method of Class Declared in Interactions

open-accepted
nobody
5
2010-08-16
2010-04-10
No

A main method in a class declared in the Interactions Pane cannot be invoked using the "java Foo" syntax:
It can be invoked using a static method call.

> class Foo { public static void main(String[] args) { } }
> java Foo
java.lang.NoSuchMethodError: main
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaConstructor.evaluate(JavaClass.java:289)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleConstructor(ExpressionEvaluator.java:128)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:98)
at koala.dynamicjava.tree.SimpleAllocation.acceptVisitor(SimpleAllocation.java:137)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
> public class Foo2 { public static void main(String[] args) { } }
> java Foo2
java.lang.NoSuchMethodError: main
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaConstructor.evaluate(JavaClass.java:289)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleConstructor(ExpressionEvaluator.java:128)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:98)
at koala.dynamicjava.tree.SimpleAllocation.acceptVisitor(SimpleAllocation.java:137)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
> public static class Foo3 { public static void main(String[] args) { } }
Modifier static is not allowed here
> Foo.class.getDeclaredMethods()
{ public static void Foo.main(edu.rice.cs.dynamicjava.interpreter.RuntimeBindings,java.lang.String[]) }
> Foo2.class.getDeclaredMethods()
{ public static void Foo2.main(edu.rice.cs.dynamicjava.interpreter.RuntimeBindings,java.lang.String[]) }
> Foo.main(new String[])
>

Discussion

  • Mathias Ricken

    Mathias Ricken - 2010-08-10

    It seems like the interpreter inserts a hidden argument of type edu.rice.cs.dynamicjava.interpreter.RuntimeBindings to static methods.

    Our interpreter has serious problems with reflection, and this has to do with reflection. We could probably hack the code that is executed when the "java" command is used to also accept a void main(edu.rice.cs.dynamicjava.interpreter.RuntimeBindings,java.lang.String[]) method.

    Welcome to DrJava. Working directory is /home/mgricken/Desktop
    > class Foo { public static void main(String[] args) { } }
    > java Foo
    java.lang.NoSuchMethodError: main
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaConstructor.evaluate(JavaClass.java:298)
    at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleConstructor(ExpressionEvaluator.java:128)
    at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:98)
    at koala.dynamicjava.tree.SimpleAllocation.acceptVisitor(SimpleAllocation.java:137)
    at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
    > Foo.class
    class Foo
    > Foo.class.getDeclaredMethods()
    { public static void Foo.main(edu.rice.cs.dynamicjava.interpreter.RuntimeBindings,java.lang.String[]) }
    >

     
  • Mathias Ricken

    Mathias Ricken - 2010-08-16
    • status: open --> open-accepted
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.