Could not find main class in Netbeans - java

package abc ;
class Trying
{
Trying ()
{
System.out.println("hello");
}
}
public class trying {
public static void main(String[] args) {
new Trying () ;
}
}
In this when I change the name of class from Trying to some other name it works , but here it says :
Error: Could not find or load main class abc.trying
/Users/name/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Why is this happening ?
I didn't find such case in any of the questions already asked .

Java is case sensitive language, but there is no documentation that class name should be case sensitive or not.
In eclipse it will show you syntax error
Class file collision: A resource exists with a different case:
'/sample/bin/abc/Trying.class'.
OR
If not shows error it will create class file of only one class either Trying or trying.
1)
If class file of Trying class is generated then It will throw
Error: Main method not found in class abc.trying
Since there is no main method in class Trying, And at runtime it looking for main method to start.
2)
If class file of trying class is generated then It will throw
Exception in thread "main" java.lang.NoClassDefFoundError:
here at runtime it looking for class Trying since it called in main of class trying. It fails to load coz its not compiled.
So we can conclude java not allow two class with sameName even different case
more details of case sensitive of class name is here

class Trying
{
Trying ()
{
System.out.println("hello");
}
}
public class Try_Main {
public static void main(String[] args) {
new Trying () ;
}
}
Please use two different class names other than same name with different cases. On compilation, the compilation will success and compile will create two class files with same name but different cases. But, the OS only allows a single file and it simply overwrites first one( which created firstly on compilation, then second) by the second one. On running, you will get a run time error, because one of the classes is missing. So, please use different names...

Well, this class should be public and be sure of saving the file name as the class Name

You have specified Trying multiple times.
package abc;
public class Trying {
public static void main(String[] args) {
trying1();
}
public static void trying1() {
System.out.println("Good?!");
}
}

Related

Why a java program with multiple main methods is not giving any error?

File Name = multiple_main_methods.java
class multiple_main_methods_two {
public static void main(String[] args){
System.out.println("Class second");
}
}
class multiple_main_methods_one {
public static void main(String[] args){
System.out.println("Class first");
}
}
Output
Class first
IDE used - IntelliJ IDEA
IntelliJ is choosing a class to execute.
Remember, you execute the main method in a class, not in a file.
Check it in your Run configurations
enter image description here
It is not giving error because your main methods belong to different classes i.e. multiple_main_methods_two and multiple_main_methods_one which is completely fine.

Error: Main method not found in class Main, please define the main method

I need help with the main method, I'm getting this error: while compiling the Project:
Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Also tried online Java compilers but no luck.
public class Main{
String yourname = "Ayush Sahu";
int mnumber = 25;
public void justtest(){
System.out.println("This is a test Method!!!");
}
}
class testing extends Main{
String fname = "Balamukunda";
String lname = "Sahu";
int age = 25;
public void testing(){
System.out.println("This is a test Method!!!");
}
public static void main(String[] args){
//Accessing the Data of Class: Main
testing myObj1 = new testing();
System.out.println(myObj1.yourname);
System.out.println(myObj1.mnumber);
myObj1.justtest();
//Accessing the Data of Class: testing
myObj1.testing();
System.out.println(myObj1.fname);
System.out.println(myObj1.lname);
System.out.println(myObj1.age);
}
}
If you want to run in IDE, right click in middle of testing class and find Run -> Run Application it the dropdown menu and click it. Try to separate each class in individual files. So you must have at least two files: Main.java and testing.java. I strongly recommend you to use Camel Case for class names and respect coding standard (which mean change testing to Testing or even better to Test).
If not, You must change it in Manifest file or find run configuration in your IDE.

main method failed to execute glitch?

Was just testing a simple code, and it appears my eclipse just got worse. This code is suppose to output 2. But when I run it, very weird error says 'Error: Main method not found in class jasc1, please define the main method as: public static void main(String[] args)' when my main method is clearly defined.
Does anyone know what this error is all about??
public class jasc1 {
int a = 2;
public void abc(){
System.out.print(a);
}
public static void main(String[] args){
new jasc1().abc();
}
}
This works fine for me, your file name must be wrong. It MUST be the same as the class name.
Additionally
Class names should (by convention) begin with an upper case letter so Jasc1
If you want to execute a class It should have a method public static void main(String [] args) (or similar meanings). But, for executing a class you have to run that class.
ex: In command line you call java jasc1 after compiling it with javac jasc1.java (ofcourse there are some options like -cs; see help)
The same way you can run one class in Eclipse or NetBeans IDEs by right-clicking on it in the project explorer and select Run or Run as
Of course, this jasc1 class cannot call another class that have a public static void main(String []args) method.

java method in seperate class file giving no symbol error

I have set up a class in a second file which is in the same package as the main but for some reason I am unable to call it without it giving a "cannot find symbol error" even though I'm sure everything is ok. This is a basic file I tried and it replicated exactly the same but im not sure if its my code or netbeans.
package filesystem;
public class FileSystem {
public static void main(String[] args) {
FileMethods(Hello);
}
}
and the other class
package filesystem;
public class FileMethods {
public void FileMethods(String myString){
System.out.println(myString);
}
}
This is the error i get next to the line
!
thanks for any help
Maybe something like this:
new FileMethods("Hello");
You cannot call a constructor directly without creating a new object.
Maybe you wanted to do
new FileMethods("Hello");
To print the string Hello.
Here you are trying to reference the variable Hello which doesn't exists.

Two public classes in one file java

Ok, this might be kiddies question in java. We can't define two public classes in one file. But, in one of the examples from the book SCJP study guide, this example was mentioned:
public abstract class A{
public abstract void show(String data);
}
public class B extends A{
public void show(String data){
System.out.println("The string data is "+data);
}
public static void main(String [] args){
B b = new B();
b.show("Some sample string data");
}
}
When I copy pasted this into netbeans immediately compile error was thrown, that public class A should me mentioned in separate file. Is that example from SCJP styudy guide really wrong? Also in some of the mock test I found many questions having such pattern but in none of the options was a compiler error was mentioned. Getting worried here
yes, 2 top level public classes are not allowed in one file
Well, if one is being so picky: you can have multiple classes defined with a public modifier in the same file, that is, using the static nested(inner) class.
like this:
File -> Test.java
public class Test {
public static class SomeNestedClass {
}
}
Yes you can have two classes in the same file. You can have them by removing the public access modifier from both the class name, like shown below,
abstract class A{
public abstract void show(String data);
}
class B extends A{
public void show(String data){
System.out.println("The string data is "+data);
}
public static void main(String [] args){
B b = new B();
b.show("Some sample string data");
}
}
you can make 2 public classes in one file , inside a class that contains them .
it's also recommended to add "static" for them , if you do not need any reference to the container class .
You can put two public classes in one file, for example in the file Circle.java:
public class Test {
public static void main(String args[]) {
double cir = Circle.findCircumference(7.5);
System.out.print("Circumference of circle=" + cir);
}
}
public class Circle {
public static double findCircumference(double radius) {
return 2 * Math.PI * radius;
}
}
If you then run javac Circle.java, you will get an error:
Circle.java:1: error: class Test is public, should be declared in a file named Test.java
public class Test {
^
1 error
But if you run it with java Circle.java, then it will work.
Why? Probably because the java command, since java 11 (see here), can run also single source-file programs.
Imagine you could place two public classes in one file, then think about the work of the compiler: it has to build a .class file from your .java file that represents exactly one class (otherwise the .class ending wouldn't make any sense).
The way the JAVA Compiler works it will simply create a .class file with the name of your file and will search for the class with the name of the file in your given file – so it depends on your file name which class will be correctly compiled and which will not.
Long story short: no, you can't put two public classes in one file because the compiler wouldn't be able to handle that correctly.
(Edit: it of course is possible to define new classes INSIDE the one public class that has the same name as your file.)

Categories

Resources