0

It gives an error "Could not find the main class: filename.java" How do I set the filename to be independent of the class names?

6
  • 8
    Use a language other than Java? Feb 3, 2012 at 18:48
  • 2
    Why would you like to do that? If you elaborate more about the concrete problem for which you thought that ignoring the filename would be the solution, we may be able to propose the right solutions for that (there may not necessarily be any solution if the problem is just caused by developer's own ignorance or laziness, though).
    – BalusC
    Feb 3, 2012 at 18:48
  • 1
    Oh, please don't. That would mess with anybody ever looking at your code. Feb 3, 2012 at 18:49
  • 3
    I would help if you elaborated a bit. What is "it"? What command do you execute?
    – JB Nizet
    Feb 3, 2012 at 18:49
  • I keep version numbers in the filename.
    – nnn
    Feb 3, 2012 at 18:52

5 Answers 5

12

You can't ... In Java the file name has to match the name of the public class in the file

See Why are filenames in Java the same as the class name? for an explanation

6
  • If I have multiple classes, how should I name the file?
    – nnn
    Feb 3, 2012 at 18:49
  • 1
    @nnn: Make one file per class or make other classes non-public? Feb 3, 2012 at 18:51
  • Don't put multiple classes in the same file unless you have a good reason to.
    – Dodd10x
    Feb 3, 2012 at 18:51
  • Have one single file for each class ... That's Java Best practice Feb 3, 2012 at 18:51
  • "one file per class or make other classes non-public" that works, thanks.
    – nnn
    Feb 3, 2012 at 18:53
4

Short Answer: You can't. One class per file is the java way. Accept that or find another language.

Longer Answer: You can but you probably don't want to.

If you have one public class and x number of non-public classes, you can put the all in the same file by nesting the non-public classes inside the public class. For example (in BlowFish.java):

public class BlowFish
{
  class Hooty
  {
  }

  class Sushi
  {
  }
}
3

you can't. a java class must be in a .java file with the same name.

0

maybe is into the manifesto file where you are defining this class as your main class

0

Sure you can. Just put version numbers in your classnames, as well.

Or keep the newest as the classname.java, with older versions getting version numbers.

Or drop the version numbers and use source code control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.