Could not find or load java main class - java

I have a very basic program in java,the filename is MainClass1.java
import java.util.Scanner;
class Student{
String name;
int roll_number;
double marks;
Student(String s,int r,double m)
{
this.name=s;
this.roll_number=r;
this.marks=m;
}
}
class MainClass1{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String name="Sourav";
int roll_number=25;
double marks=70.50;
Student student1=new Student(name,roll_number,marks);
System.out.println("The student is "+student1.name+" and his roll number is "+student1.roll_number+" and his marks is "+student1.marks);
}
}
It compiles fine,however when I am trying to run it by
java Mainclass1
it shows an error
Error: Could not find or load main class Mainclass1
Caused by: java.lang.NoClassDefFoundError: MainClass1 (wrong name: Mainclass1)
I know its a very basic issue,but unable to figure out the issue.
Please help anyone

When you run a program (that is, when you call java ...) the JVM needs to know where to start running the code. It looks for a public static method named main, which takes an array of String as input, and is defined on whatever class is named in the .java file.
So:
verify that your file is named "MainClass1.java" – the filename and the class name need to match
verify that the class includes the public modifier, so change this:
class MainClass1 {
...
}
to this:
public class MainClass1 {
...
}

Related

Instantiating Objects in Java

I asked a similar question to this recently, but I did a bad job at explaining it, so I am going to try again.
I cannot figure this out for the life of me. I have to do two different java files for this programming assignment, and when running the program from command prompt I get an error that says it cannot find symbol of when I create my object in the second class, and when I run the code in Eclipse I get the error "Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package jdk.internal.jrtfs in both module jrt.fs and module java.base".
I even copied an example straight from the textbook to understand it, but it isn't working even though I have exactly what's in the textbook. The pastebin links are for the two classes that come from the textbook examples.
Please someone tell me what's wrong here.
First class: https://pastebin.com/KYHtDHPt
public class Account {
private String name;
public Account(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Second class: https://pastebin.com/ADUsjjaR
public class AccountTest {
public static void main(String[] args) {
Account account1 = new Account("Brandon Williams");
System.out.printf("Account one is: %s%n",account1.getName());
}
}

Program is not compiling for Java what am I Missing

package freshjuice;
class FreshJuice {
enum FreshJuiceSize { SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
}
public class FreshJuiceTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
// TODO code application logic here
}
}
This is the error message I am getting:
Error: Main method not found in class freshjuice.FreshJuice, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
C:\Users\TheGODMasterDu\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
Your file must be called FreshJuiceTest.java, because that is the class where your main method is located.
Your file name is FreshJuice.java which does not contains main()
In short netbeans looking for main() in your first class
Either change your file name to FreshJuiceTest.java or swap the code of your classes and define FreshJuice.java as public and remove public from second class

Static Variable becomes null after class completed

I Have three classes
StaticHolder.java - Which holds a static variable.
StaticInitializer.java -Responsible only for initializing the variable through a static method.
Application.java - Retrieves the static variables value through getter method.
I thought initializing a static variable once in JVM will not go until we stop the JVM. So I called ran StaticInitializer once which will do the initialization. And tired to access its value from another class which is not working and returning null. Can anyone explain why. Thanks In Advance.
public class StaticHolder {
private static String hello;
public static void ini() {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
class StaticInitializer {
public static void main(String[] args) {
StaticHolder.ini();
while (true) {
Thread.sleep(1000);
}
}
}
public class Application {
public static void main(String[] args) {
System.out.println(StaticHolder.getHello());
}
}
static does not mean that this value is there forever!
It is only theree for the current java session.
Invocing the java command at the command line starts a new java session where the value needs to be initialized again.
Actually I have a daemon thread which does the initialization and stays alive.And I have another stand alone java program which tries to get the value.
Without knowing that other code involved my gueass is that you did not establish inter process communication.
The easiest way it that you "deamon" opens a server socket and your "stand alone java program" connects to it an queries the desired data through it.
So there is only one main method that can be executed as entry point for the entire application for each JVM run.
When the JVM is executed you can specify which class has to be loaded at start. The Classloader take care to load that class and then the JVM can execute the only one public static void main(String[] args) method.
In Java you need to have at least one class with a public static method named main. I suggest to read this post to understand why it is public static.
The Java Classloader is a part of the Java Runtime Environment that
dynamically loads Java classes into the Java Virtual Machine.
Usually classes are only loaded on demand.
So returning to your question, given that when Application.main is running there is no way to execute StaticHolder.init(), I suggest to change your main in this way:
public class Application {
public static void main(String[] args) {
StaticHolder.init();
System.out.println(StaticHolder.getHello());
}
}
or change StaticHolder in this way and remove the init:
public class StaticHolder {
private static String hello;
static {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
On the other hand, just to be clear if you run the StaticInitializer.main this has no effect on Application.main execution.
In your program , when main method of StaticInitializer is first executed, a String named hello is initalized. and as ini() method is called, the value 'Hello world' is assigned to hello. Then jvm exists main method, and then stops working. Again when we compile application class,instead of the previous hello variable , a new hello string variable is created with no value assigned(null valued) . That's why you're getting null as output. Thankyou.

Setting -classpath in bat file

I am trying to create a compile.bat file using the following classes: HumanTest (main method), Man, Food. Below are the code for the 3 classes. In this situation Food is already compiled and I do not have the .java file for it.
package human.man;
public class Man {
private String name;
private Food f;
public Man(String name, Food f) {
this.name = name;
this.f = f;
}
}
public class Food {
private String foodName;
public Food(String name) {
foodName = name;
}
}
import human.man.*;
public class HumanTest {
public static void main (String[] args) {
Food f = new Food("ckt");
Man m = new Man("joe", f);
}
}
In compile.bat, i run the following code
javac -cp classes;src HumanTest.java
But I get the error that Food class cannot be found. I'm wondering why this is so even though I have already set the classpath for Food.class. Here's the link for the files: https://www.dropbox.com/s/2wussnm55tbnh3t/Question.zip?dl=0
EDIT:
Below is the tree diagram, do let me know if I drew it incorrectly!
--Question
|--compile.bat
|--HumanTest.java
|--classes
|--Food.class
|--src
|--human
|--man
|--Man.java
The problem is not related to batch files, but purely on the organization of your classes. You can't import a default package from a named package, nor can you use a class that is in the default package from a class that is in a named package.
In class human.man.Man, you are trying to use class Food which is in the default package. Try moving Food to a named package instead.

Exception in thread "main" java.lang.NoSuchMethodError: main - how to fix it?

Please help me to fix my simple program. I'm just newbie of Java Programming. When i write the program, and the program has been success compile but can't run cause the massage error "Exception in thread "main" java.lang.NoSuchMethodError: main" has been show. Everybody can help me to run this program ?
The coding is:
Person class...
//Program to display student details using inheritance
class Person {
String name;
int age;
}
Student class...
class Student extends Person
{
int mark1,mark2,mark3;
void putdata()
{
System.out.println("Name = " + name);
System.out.println("Age = " + age);
System.out.println("Mark1 = " + mark1);
System.out.println("Mark2 = " + mark2);
System.out.println("Mark3 = " + mark3);
}
}
Marks class...
class Marks
{
public static void main(String[] args)
{
Student obj1=new Student();
obj1.name="Sultanah";
obj1.age=17;
obj1.mark1=67;
obj1.mark2=87;
obj1.mark3=97;
obj1.putdata();
}
}
JVM when try to launch the program looks for the class with the same name as the name passed to run the program. Once the class is found then it looks for the main method in it. So make sure that you are running the program using Mark as the class name because that is the class that contains your main method.
Either:
Your main() method shoudl be placed in the Student class, if that's the class you're executing, or
You should be executing the class that does contain the main() method, i.e. Marks.
It seems you trying to run class that doesn't have main method. according to your code Marks.java has main method so after compilation you can run Marks class using
java Marks

Categories