Make two Java file separate from each other in one folder [duplicate] - java

This question already has answers here:
File name and class name different in java
(1 answer)
Can I compile a java file with a different name than the class?
(21 answers)
Closed 6 months ago.
I usually do my homework on a Java file and submit the file to complete my task, so I separate each file and contain them all in one folder. Sometimes there are two works that have the same class name, but different work. If I let two files have the same class name, they won't run properly. (I'm using vscode)
For example:
File1.java
public class File1{
public static void main(String[] args) {
...
}
}
class Fraction {...}
File2.java
public class File2{
public static void main(String[] args) {
...
}
}
class Fraction {...} //this is not the same with File1.java
Can I do anything keep two class names the same in two different files in one folder?

Do not define the same class name (whether the same script or not) in the same folder. This throws an error many times.

Largely all filenames are Operating System rules dependent.
Java source code files must be named by the main class name of the code in the file. After compiling, a file of the same name but different extension, extension of .class will be there. Dependent how the compiler command line flags are set it will simply by default overwrite any old version .class file.
type javac -h on the shell or prompt for compile command flags info.

Related

Is Empty .java file name working?

I started to learn Java couple days ago. And I have this burning question. Is empty .java file name a valid source file name?
.java
Yes, save your java file by .java then compile it by javac .java and run by java yourclassname
class X {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
to compile - javac .java
to execute - java X
Yes it's working because java compiler doesn't consider it saves file name or not except our class having public specified we can save any name or empty but when ever trying to execute we must use our class name because, jvm creates byte code ourclassname.class so we using
java className
Yes Empty .java file name works, but class must not be public, it means that it must be default.
If class is public then following error occour:
D:\Testjavac>javac .java
.java:1: error: class Empty is public, should be declared in a file named Empty.
java
public class Empty
^
1 error
Yes You can have .java file withought nay name . you have to compile it by javac .java(it compile successfuly) and run it by java clasnname.(so you must provide a class name)
Yes, but don't do this often.
You can't create any classes in that file that are public or private, so any class that made use of any class defined here would have to be in the same package.
at anytime you can have only one public class in the file and if you use public class then that class name should be the file name.

Java Class Saved with another name? [duplicate]

This question already has answers here:
Error: class X is public should be declared in a file named X.java
(19 answers)
Closed 9 years ago.
class Blog{
public static void main(String args[]){
System.out.println("overflow");
}
}
I am saving this file with name First.java and compiling it than it is generating file with Blog.class and gives output:
overflow
If same program i am writing as given below:-
public class Blog{
public static void main(String args[]){
System.out.println("overflow");
}
}
it gives error after compiling
First.java:3: error: class Blog is public, should be declared in a file named Blog.java
If you declare a class as public then the file name and the class name should same.Otherwise you will get compile time error.
So change you file name to Blog.java from First.java.
The reported error is really clear:
class Blog is public, should be declared in a file named Blog.java
java classes always should have same name with its file name (case sensitive) with .java extension. so you should save it Blog.java
Because it is part of the Java Language Specification! You may also have a look at Managing Source and Class Files in the Java Tutorials.
From the JLS
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 (such as .java or .jav) if either of the
following is true:
•The type is referred to by code in other compilation units of the
package in which the type is declared.
•The type is declared public (and therefore is potentially accessible
from code in other packages).
This restriction implies that there must be at most one such type per
compilation unit. This restriction makes it easy for a Java compiler
to find a named class within a package. In practice, many programmers
choose to put each class or interface type in its own compilation
unit, whether or not it is public or is referred to by code in other
compilation units.

why to save the name of java program with the same main class name in case of public main class [duplicate]

This question already has answers here:
Why are filenames in Java the same as the public class name? [closed]
(7 answers)
Closed 9 years ago.
why is it necessary to save the name of java program with the main class name when that class is public, if the class is not public then we can save the program with any name and then can compile it. why in case of public main class we can not do that.?
I think the question means, why does a public class name needs to match the name of the file it's been declared in.
I think this answer by Bill K would answer your question:
https://stackoverflow.com/a/2134888/1401409
No it is not necessary . You can save the file containing class say MainClass containing main method with any other name say NonMainClass.java (given that the file doesn't contain any other public class). You can compile the same using javac NonMainClass.java . But when executing it you need to execute it using java MainClass.
For Example. Consider the code given below:
class MainsClass
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
This file is saved as NoMainClass.java. And it is compiled as javac NoMainClass.java. It generates the MainClass.class in working directory. And to execute the program you need to run following command java MainClass .

Briefly, main function behavior - multiple classes in one source file

Given a Java source file named as TestMainFunction.java
1.) As most of us would know, a Java file name must be the class name which contains the main function
See the below simple code (in a single file named as mentioned above) which executes perfectly and prints ClassOne.
public class TestMainFunction {}
class ClassOne {
public static void main(String[] a) {
System.out.println("ClassOne");
}
}
2.) When there is more than one class with a main function in a single file, Eclipse prompts the user to choose the class to run.
See the below simple code (single file named as mentioned above) which executes perfectly and prints the class name as we have chosen from the Eclipse promt.
public class TestMainFunction {
public static void main(String[] a) {
System.out.println("TestMainFunction");
}
}
class ClassOne {
public static void main(String[] a) {
System.out.println("ClassOne");
}
}
class ClassTwo {
public static void main(String[] a) {
System.out.println("ClassTwo");
}
}
All the above will work as I mentioned. It will create separate .class file for every class. The same behavior applies even when using inner classes.
But what exactly is happening here? How does Eclipse know which class to launch when multiple classes are present in one source file? Can any one explain it? Explaining this would be greatly appreciated.
As most of you would know, a Java file name must be the class name
which contains the main function
This is incorrect, and I believe the source of your confusion. A Java source file is only allowed to have one public class, and it must have the same name (minus the extension) as it's containing file. A given Java source file though, may contain as many non-public class files as desired, with the only constraint being that their names are valid. Note that you can have a class with the same name as its containing source file (minus the extension) that is not public! Also note that it's generally considered bad practice to have multiple top-level (non-nested) classes in a single Java source file.
The second assumption you may have is that only one class in a source file is allowed to have a main function. This is simply untrue. You can add a main function to every single one of your class files - the only one that matters is the one you specify to the JVM when your application is being launched.
So given your scenario - a source file with multiple class files (one of them public), where each class has a main method, the following applies:
When you invoke the Run command in Eclipse on this file, Eclipse will detect that there is more than one eligible class to be run, and will prompt you to select one of those class. It will then build a 'Run Profile' for the selected class, that launches just that class. You can actually see the profile Eclipse builds via the Run->Debug Configurations menu.
When compiling this source file externally, it will generate multiple .class files (not just one). The classes had no relation to each other save being in the same source file, and you would explicitly select the .class you want to launch in the JVM.
i.e:
java TestMainFunction
java ClassOne
java ClassTwo

How to run Java .class file from another .class file? (java newb)

I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}

Categories

Resources