tags:

views:

368

answers:

9

Is there any way to compile a java program without having the java file name with its base class name.

If so, please explain..

A: 

I guess what he means is the .java file is named differently than the actual class defined inside it.

I guess this is not possible.

Kalpak
+1  A: 

No, the public class name must match the file name. Inner, non public, class names may differ.

Joel
+2  A: 

You must have a public class with the same name as the file name. This is a Very Good Thing. You CAN have secondary classes inside the same file as long as they are not public. They can still be "default" though, so they can still be used by other classes in the same package.

This should not be done for the most part. Java's naming patterns regarding classes and packages are one of the bigger advantages it has--makes a programmers life easier at no cost.

Bill K
+10  A: 

Your Java file name should always reflect the public class defined within that file. Otherwise, you will get a compiler error. For example, test.java:

public class Foo {}

Trying to compile this gives:

[steven@scstop:~]% javac test.java
test.java:1: class Foo is public, should be declared in a file named Foo.java
public class Foo {
       ^
1 error

So you must have your filename match your public class name, which seems to render your question moot. Either that or I don't understand what you're asking... spending some time explaining what you are actually trying to achieve would go a long way towards asking a more effective question :)

Steven Schlansker
Thanks....you are exactly rit.....I know i have to save the file with public class name to compile it......but is it possible no to do this and compile the java program file with sum other name on it.....
Hulk
No. The compiler won't allow you to. Please don't try to get around this restriction - as others have mentioned it is a very good thing that Java requires this! If you are trying to achieve some specific goal, if you explain more about what you are trying to accomplish we could suggest alternative methods.
Steven Schlansker
If you want to your program to have a certain name, you need to either change the name of your class or create a new class that will execute the program.
A: 

No. You could write a shell script to rename the .java file before compiling it, but javac requires that filenames = class names.

(Also in windows, it's case insensitive, so ASDF.java can compile Asdf.class)

Chad Okere
A: 

You can use the Java Compile API and compile any java source you wish, the source need not come from a file or could come from a file with an unrelated name. It depends on how obtuse you want to develop your program. ;)

Peter Lawrey
A: 

yes, you can choose any name for the file (.java). there is no matter to what are the name of classes in that file means that class names may be totaly different from the file name. you should compile the program with file name and you should run the program with the class name in which the main method exist. main methods may be multiple in different classes so you should run it with the class name in which the main method you want to run......

ved
If I understood you correctly, you are wrong - but maybe an example would be helpful to straighten things out.
Till
as i know i m not wrong. for example create a file sample.java with coding class A { public static void main(String args[]) { String str={""}; System.out.println("hi"); B.main(str); } } class B { public static void main(String args[]) { System.out.println("hello"); } } now u compile it as javac sample.java and run as java A then output will be hi hello
ved
+1  A: 

as i know i m not wrong. for example create a file sample.java with coding

class A 
{ 
   public static void main(String args[])
   { 
      String str={""}; 
      System.out.println("hi"); 
      B.main(str); 
   } 
} 
class B 
{ 
   public static void main(String args[]) 
   { 
     System.out.println("hello");
   }
} 

now u compile it as

javac sample.java 

and run as

java A 

then output will be

hi 
hello 
ved
This works because neither 'A' nor 'B' are public classes. If A was, it would have to be defined in A.java.
DoctorRuss
Nice one............
Hulk
A: 

we can save the file tootle different name of class name because in java we compile the program but we run the method. we have to compile our program with file name and run our class name