Can't we run main method inside enum? [duplicate] - java

This question already has answers here:
Java can't find main class
(5 answers)
Closed 6 years ago.
I had seen a video where, main() can be run with in an enum.
I am trying to do the same but it's not working.
Here is my code
public enum EnumMain {
ABC, XYZ;
public static void main(String[] args) {
System.out.print("MIAN");
}
}
Output
(The code compiles fine)
Error: Could not find or load main class EnumMain
I think this has something to do with the Java version, may be in Java 8, they are not allowing to run main() method from enum anymore.
PS I am compiling and running the file from windows command prompt.
Note If I change the enum to class then it runs fine (I don't have any classpath issues)

You can run main within enum.
public enum TestEnumMain{
val1, val2;
public static void main(String[] args)
{
System.out.println("Hello");
}
}
Problem would be with you your path variables. Make sure they are configured right. Refer this thread for setting path variables propertly.

No, I have run your code on Java 8 and it is perfect. It runs to give output MIAN. I think the problem lies in classpath or in your IDE. You might try after cleaning the project.

Related

Java application start point [duplicate]

This question already has answers here:
Entry point for Java applications: main(), init(), or run()?
(5 answers)
Closed 4 years ago.
I downloaded a simple java app and I want to know which file I need to run to start. There are several classes in the project and I do not know which one is the main one. Thank you.
Search for the class with the main method, that is the method where program execution starts. It looks like
public static void main(String[] args) { ... }
It's inside the class Main.java of your project:
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
// [...]
}
Open the project in an IDE and run this class. Or do it manually in the console like:
javac Main.java
java Main
Only .jar files can automatically be started like .exe files. Your project only contains code and you will need to compile and run it on your own.
You should have public static void method called "main" taking array of String as parameter in one of the classes. Something like below
public static void main(String... args) {
System.out.println("test");
}
This is your entry point.
#Edit
OP added code so here is an answer update:
Your entry point is in class Main and method is
public static void main(final String[] args
If you're using intellij just run click on this method and select Run "Main.main()"

Eclipse run configuration

When i'm trying to run in Eclipse program which has no main method as Java Application it always requires a main method to define. Could anybody please explain is there any simpler way to make configuration run without implementing
public static void main(String[] args) {
}
Every program requires a main method.
The public static void main(String args[]) starts the main thread which your program will run on.
There's more information available here.
You need a main method to run your program.
public static void main(String[] args){
YourClass newObject = new YourClass();
}
Put this method in your class (replace YourClass with your existing class) and hit the run button.
run in Eclipse program which has no main method
NO.You won't be able to run a class by using IDE like Eclipse or anything else. Even you won't be able to run your java program by using Java command from your command prompt, until your java program has public static void main(String args[]) method on it. If you try to run, you will get below mentioned error
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
But you should also know that, we can run a Java program without main method on managed environment like the case of Applet, Servlet and MIDlet. There are actually different types of execution model available in Java, for example Applets which run on browser doesn't have main method. Servlet is also java program, which runs in a Servlet container and since Servlet is also a Java program, we can say that it runs without main method. Third one on this category is MIDlet, which runs on mobile devices.

Getting NoSuchMethodError at runtime

I have looked through many answers to similar questions. But couldn't narrow down to a solution.
Following is the code: (Simplifying names for readability)
First class:
package p1;
public class C1 {
public static void test() {
System.out.println("Boom!");
}
}
Second class:
package p2;
import p1;
public class C2 {
public static void main(String[] params) {
C1.test();
}
}
Clean-Build doesn't give any error. (No compilation error)
But at runtime I'm getting following error:
Exception in thread "main" java.lang.NoSuchMethodError: C1.test()V
at C2.main(C2.java:6)
Java Result: 1
P.S. I'm using Netbeans.
This means that you are running your class C2 with an old version of class C1 in the classpath (a version that did not yet have the test() method).
Make sure you don't have old versions of C1.class somewhere. Remove all your *.class files and recompile everything, and then try to run it again.
Addition: As Kevin Bowersox noted in a comment, your main method must look like this:
public static void main(String[] args)
It must take a String[] as an argument.
It will properly compile and run only if main function will have String tab as args.
But also check versions of class C1 and C2, try rebuild your project to recompile that classes.
public static void main(String args[]) {
C1.test();
}
i think you should import it as
import p1.*;
Than you will get access to all classes and member functions in it.
Netbeans sometimes likes to get stuck after some changes and clean build doesn't work then.
Try editing each file that has been recently modified and saving it again (e.g. put a whitespace in a random place). After that, clean and build the project again.
If my memory refreshes and as Jesper pointed out, I also encountered that same issue NoSuchMethodFoundException under that same scenario (having still old class references that have not been cleaned).
I just copied your code snippets with 2 different packages directly in to my netbean, compiled and runned C2. It did print the BOOM! message.
In my case using :
public static void main(String args[]){
}
does not make a difference when I compiled and runned the code.
public static void main(String params[]){
}
It makes sense since the main class should have the correct method signature of main.
Here args or params, should not make a huge difference, I believe; as what we have inside the method is simply a reference for the inner body of the method that it uses.
Still definitely it is good practice to follow the standard signature for main.
I would recommend to clean the project and copy the contents from scratch in a new project and build it again, sometimes netbeans can go crazy.

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);{
}

Exception in thread "main" java.lang.NoSuchMethodError: main [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main
I got the above message. The code is as follows:
class Test
{
public static void main(String ar[])
{
printf("hai");
}
}
How is this problem caused and how can I fix it?
The class which you're trying to execute doesn't have a main method.
Since your main method looks syntactically fine, this can have two causes:
You're executing the wrong class.
The actual class file doesn't contain this code.
The solution is obvious:
Make sure that your command is pointing the correct class file, you might have multiple class files with the same name and be sitting in the wrong directory.
Make sure that you've compiled the correct source file into the correct class file before, you might have edited one and other and forgot to recompile.
In addition to the problem that's causing the current exception (see BalusC's answer), the proper "Hello World" in Java is:
class Test
{
public static void main(String[] args) {
System.out.println("hai");
}
}
See: java.lang.System
I see your problem, the signature is not correct.
It should be public static void main(String[] args)
It could also be a class path issue which causes Eclipse to get confused and not able to find your class when it tries to run it. I would look at the Java Build Path in the Project Properties to make sure there are no errors.

Categories

Resources