开发者

Using Windows device name such as Con as Java class name

开发者 https://www.devze.com 2023-03-25 11:00 出处:网络
I am referring to my previous question but this time I used the java compiler and the compiler compiles the output- it gives a weird output. And this time I used this instead of super.

I am referring to my previous question but this time I used the java compiler and the compiler compiles the output- it gives a weird output. And this time I used this instead of super.

Using Windows device name such as Con as Java class name

This is the code of the program.

class Con {
    int x = 10;
    Con() {
        this(2);
        Syst开发者_StackOverflow社区em.out.println("x :" + x);
    }
    Con(int i) {
        x = i;
        System.out.println("x :" + x);
    }   
}

class DemoCon {
    public static void main(String args[]) {
        Con c1 = new Con();
    }
}

What do you think is the problem here? Is this a bug in Java?

Java version - 1.6.0 JDK

I used Eclipse to run the program and there is a Class not found exception. A.java is the file name... We did a minor edit and made a public class called A.java too but the results are same. We further found out that the problem lies in the compiler.


On Windows it seems CON is reserved name and cannot be used for folders/directories or filenames.

The following

print "test" > Con.java

is not working.

Therefor the compiler is unable to create your Con.class and crashes.

From MSDN:

Do not use the following reserved device names for the name of a file:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended


perhaps the problem exists because CON is a reserved file name (it was on MS-DOS -- see http://support.microsoft.com/kb/31157 http://www.computerhope.com/copyhlp.htm)


How did you compile it? On Win7 32b with Java 1.6 I get:

Type name is not valid. 'Con' is an invalid name on this platform.


Yes it looks like a bug. It compiled very well on my

Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)

environment on Mac OS X.

Maybe we can help further if you tell us whether you are using Sun (Oracle) JDK or OpenJDK?

Problem might be about class name (Con) and file name (A) are different (they should be the same) and you have two classes at the same level in a single file. Anyway, it compiles well on my box.


In java, the class name and file name must be exactly the same. In your case, your class name is Con, therefore, your class file must be Con.java. Since DemoCon is your class with the static void main(String[] args), your java file must be DemoCon.java.


I saved your code as DemoCon.java .

and ran it as

javac DemoCon.java 

java DemoCon

o/p was

 x :2
    x :2
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号