Cannot run simple java class

Project:JNode Core
Component:Code
Category:bug report
Priority:normal
Assigned:Stephen Crawley
Status:closed
Description

Step to reproduce :

Start Desktop
use editor to create simple helloworld application
save it to /jnode/home
compile it
run it

see screenshot.

AttachmentSize
Screenshot.png81.76 KB

#1

Maybe this is a silly question, but did you declare the FirstApp class to be public?

#2

No, if i declare class to be public it's work. But by default a class is public if there is no keyword, isn't it ?

#3

No. If you leave off the 'public', a classes access is "package private"; i.e only other classes declared in the same package have access. So JNode is doing the right thing here. The only issue here is that the "java" command ought to produce a better error message,

#4

Just for interest's sake, here's the output of a similar experiment on Windows:

C:\Users\daniel\Desktop\test>cat Test.java
class Test {
    public static void main(String[] args) {
        System.out.println("Hi there.");
    }
}

C:\Users\daniel\Desktop\test>javac Test.java

C:\Users\daniel\Desktop\test>java Test
Hi there.

So whether Jnode is doing the "right thing" comes down to whether Sun's java executable is doing the right thing. Perhaps Sun are violating the spec. I haven't read the spec so I don't really know.

#5

Assigned to:Anonymous» Stephen Crawley

Hmmm ... yes I see. I'll try and fix JavaCommand tonight.

#6

This is obviusly a bug.
The rule is that if a class has no package specified then it's in the public package. All classes in the public package are public even if they have no public keyword specified in the class declaration.

#7

Interesting theory, though I cannot find anything in the JLS to say that the class access rules work differently for classes in the "unnamed" package.

Section 12.1.4 doesn't say that the JVM should pay any attention to a classes access when finding the 'main' method. Indeed the example in 6.6.5 strongly suggests that it should not.
This aligns with what the Sun 'java' command does:

[stephen@localhost tmp]$ rm Test*
[stephen@localhost tmp]$ cat foo/Test.java
package foo;
class Test {
    public static void main(String[] args) {
        System.out.println("Hi there.");
    }
}
[stephen@localhost tmp]$ javac foo/Test.java 
[stephen@localhost tmp]$ java foo.Test 
Hi there.
[stephen@localhost tmp]$ java -version
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Server VM (build 10.0-b19, mixed mode)
[stephen@localhost tmp]$

So the correct fix for JavaCommand is for it to call setAccessible(true) on the Method object for the "public static void main(String[])" method. The same fix needs to go into the invokers as well.

#8

Hm, I might have remembered incorectly then. After all the right place to check this is the JLS indeed.
The fix looks ok to me.

#9

This gave me the perverse idea to try it with a private inner class as well, which also turns out to work:

C:\Users\daniel\Desktop\test>cat Test.java
class Test {
    private static class Nested {
        public static void main(String[] args) {
             System.out.println("Hi there.");
        }
    }
}

C:\Users\daniel\Desktop\test>javac Test.java

C:\Users\daniel\Desktop\test>java Test$Nested
Hi there.

NFI why they chose to make this possible, but it's interesting that this allows you to make a completely private class runnable, thus preventing people running your main method directly without reflection or similar hackery.

#10

Status:active» fixed

<shrug>

I'm marking the issue as fixed.

#11

Status:fixed» closed

#12

How to decompile and then recompile a Java class file? I am using WinRAR to extract jar files to get the class files. I'm looking for the best Java class decompiler to decompile the class files of any jar file. After decompiling, how do I recompile the class files after editing the source code? How do I make it as a jar file again? Do I just need to zip the file and then change the extension to .jar? Please help. Thanks in advance.

#13

This not a forum for general Java development questions. Try some other site; e.g. StackOverflow. Or use Google.