1

I have a Java file containing a single public class (called Test) and a private class (called Aux).

When I call javac Test.java, I get the following error:

Test.java:5: error: error while writing Aux: C:\Users\Wolf\Desktop\java\Aux.class

I thought it might be a permissions issues, but I have Admin access to the folder (the Java JDK is on my path).

Also, if I comment out the Aux class (leaving the Test class in), it works.

Any idea why it doesn't work with multiple classes in the same file?

Here's the code for Test.java:

class Aux {
}

public class Test {
     public static void main(String[] args) {
     }
}
Wolf
  • 317
  • 3
  • 10
  • Please post your code for `Test.java`... – Zephyr Jun 25 '18 at 00:38
  • 2
    If you have a file `Test.java`, the only public class you can have there is `class Test` - If you have classes with different access modifiers, and the public one is not Test, this will not work. – dmitryro Jun 25 '18 at 00:41
  • Unrelated, but why on earth would you be doing this in your desktop directory? Tangentially related to that-use source control. Get in the habit. – Dave Newton Jun 25 '18 at 00:41
  • Thanks @dmitryro, so I can't include private helper classes in the same file as a public class? In that case, when do you use private classes? – Wolf Jun 25 '18 at 00:44
  • I'd suggest to use a package level access, declare a package for that file with two classes and provide your source structure is consistent. – dmitryro Jun 25 '18 at 00:46
  • That code works for me using the Oracle jdk. Which jdk are you using? – Paul Hicks Jun 25 '18 at 00:49
  • @PaulHicks I'm using the Oracle JDK, version 10.0.1 with Windows 10. – Wolf Jun 25 '18 at 00:51
  • Ah. I'm using 1.8.0_31. Try it there? Or at least with `-source 1.8 -target 1.8`? – Paul Hicks Jun 25 '18 at 00:54
  • @PaulHicks running with the "-source" and "-target" doesn't work, unfortunately. It does seem like this should work (and the documentation suggests that it should). There's just something frustratingly weird about my environment. – Wolf Jun 25 '18 at 01:01
  • The question title should be updated to mention "aux": this needs to be findable when, in 3 years time, it happens to some other unfortunate developer... – Paul Hicks Jun 25 '18 at 01:04
  • Good point. I'll make the change. Thanks. – Wolf Jun 25 '18 at 01:05
  • I've added a duplicate that lists more illegal file names. Note that this only is a hard-to-find problem when you put the class with the illegal name in the same source file as another public class. If you had put `class Aux` inside its own `Aux.java` class, you would have found out that it was an illegal file name, the moment that you tried to save `Aux.java`. – Erwin Bolwidt Jun 25 '18 at 01:40

1 Answers1

4

The problem is that in Windows, you cannot create a file named Aux for legacy reasons. It ought to work if you rename your class to something other than Aux.

jacobm
  • 13,790
  • 1
  • 25
  • 27