4

If a public class is defined in a file with the wrong filename, javac will throw an error:

square_supplies.java:5: error: class Answer is public, should be declared in a file named Answer.java

Is there a flag to turn off this behaviour? If it's not possible for javac, is there another java compiler where this can be turned off?

Benno
  • 5,288
  • 5
  • 42
  • 60
  • 5
    What's the reasoning behind putting the `Answer` class in a file that's not Answer.java? – blake305 Dec 02 '14 at 15:03
  • 5
    Why would you want to do that? What problem are you trying to solve? –  Dec 02 '14 at 15:04
  • 1
    For competitive programming, I have a folder with files named, say, `132-problem_name.cpp`, `43B-problem2.cpp`. I don't want to have to put every java file in its own folder when I'm switching to java for a single problem, because thats inconsistent. How does it matter *why* I want to do this? – Benno Dec 02 '14 at 15:07
  • Would it be possible to dynamically rename the file name (or write a parser to go in the .java file and rename the class to the file name)? – blake305 Dec 02 '14 at 15:08
  • What you want to do is irrelevant, that is how the packaging system works. [The naming convention for the source files and the packages ( that just happen to be represented by directories on a filesystem ) are specified for a reason and have worked for everyone since 1999.](http://www.oracle.com/technetwork/java/codeconventions-135099.html) **Use a proper build system to solve your perceived problem that is not a problem.** –  Dec 02 '14 at 15:08
  • 1
    blake305: I'm currently working around this by making Answer.java a soft link to my actual file. It's still annoying to have to do that. – Benno Dec 02 '14 at 15:12
  • http://stackoverflow.com/questions/2134784/why-are-filenames-in-java-the-same-as-the-class-name –  Dec 02 '14 at 15:12
  • @Benno remember you don't have to compile the file in your working directory. It's perfectly fine to copy your .java file to a temporary location and rename it, compile it, and copy the compiled file to wherever you'd like – blake305 Dec 02 '14 at 15:14
  • 1
    Jarrod: I'm not asking for the reason behind java naming conventions, I'm asking if it's possible to turn them off. – Benno Dec 02 '14 at 15:15
  • 1
    @JarrodRoberson This question is not really duplicate. It's not about "why it doesn't work" it's about is there any compiler option – ponomandr Dec 02 '14 at 15:16
  • 1
    its not an option period, and why it is not an option is a duplicate, asking for alternative compiler recommendations is **off-topic** either way. –  Dec 02 '14 at 15:17
  • 1
    blake305: Sure, it's possible to work around the problem. That's what I did, in fact. Then I wondered if there is a better way, so I asked this question. (Which seems to be offensive to some people? I have no idea why it is getting downvoted.) – Benno Dec 02 '14 at 15:21
  • 1
    Jarrod: The linked question is not a duplicate. This should be obvious when comparing the correct answers: "Your command line invocation was wrong." vs. "No compiler currently implements this feature.". – Benno Dec 02 '14 at 16:41
  • I have this same problem: Downloading a batch a student program submissions from the Blackboard LMS, likely all named the same thing to start with, the system mangles all the filenames with a unique ID to keep them distinguished... which the Java compiler can't deal with. (Whereas not a problem for other languages.) – Daniel R. Collins Apr 20 '23 at 04:28

1 Answers1

6

No, this is part of the java language. If the public class Answer is not in Answer.java, then the javac compiler have no way to know where the class Answer is located. And it need that information when compiling classes which depend on Answer.

MTilsted
  • 5,425
  • 9
  • 44
  • 76
  • Agreed. Think about how the ClassLoader works. It has to be able to find all of the packages and classes without loading the .class file and examining it to find the class name. – blake305 Dec 02 '14 at 15:05
  • 2
    I also thought this is part of the language specification. But the JLS does not cover the compiler at all. –  Dec 02 '14 at 15:06
  • 1
    @a_horse_with_no_name It is actually optional. It is a widely enforced convention though. [JLS 7.6 - Top Level Type Declarations](http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6) – Dev Dec 02 '14 at 15:13
  • 2
    Thanks, I missed this part: "*If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension*" –  Dec 02 '14 at 15:15
  • So, if it's optional I guess the second part of the question is still valid: Is there a java compiler which doesn't enforce this? – Benno Dec 02 '14 at 15:19
  • 2
    @Benno Not that I'm aware of, it is a widely enforced convention for good reason. Making it optional in the JLS was likely done to allow latitude for unconventional compilation scenarios. – Dev Dec 02 '14 at 15:22