Java application start point [duplicate] - java

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()"

Related

How to launch program in psvm?

How to launch a program in psvm with one command?
How does the application know which class to launch first?
I know that psvm should only have starting command and nothing more.
Could you explain this to me?
I mean how to create proper public static void main(String[] args) in a simple program on Maven. Should I create a class i.e. Starter with method run (with sequence actions) and in psvm write new Starter().run()?
psvm stands for public static void main as shown below:
public static void main(String[] args) {
// Your code here
}
psvm is not a standard Java terminology. You can call it as a Java slang. It is the entry point in your standalone Java application i.e. when you run an executable jar, it will execute the class having psvm. There are so much of content about it on the internet e.g. https://dzone.com/articles/executable-java-applications
The main() is the first entry point of Java application. Java Virtual Machine is told to run an application by specifying its class using the application launcher & it will look for the main() with exact syntax of public static void main(String[]).
Considering your comments you want to do something like this :
public class Starter{
public static void main(String args) {
new Starter().run();
}
public void run() {
//your logic
}
}
once you write this,
you have multiple options to run this I am mentioning a few
1) by building jar and then executing that jar using java -jar command
2) or by executing maven command once you have compiled your program using mvn compile, mvn exec:java -Dexec.mainClass="complete name of your main class i.e including package name."
a few links
http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Maven_SE/Maven.html
hope this might help

Java - can't run program in Eclipse

I can't run HelloWorld in Eclipse - it shows message:
Error: Main method not found in class com.taksila.javabyexample.overview.HelloWorld, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Here is my code:
package com.taksila.javabyexample.overview;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hi Planet");
}
}
Code is correct. Try to restart IDE or just save sketch :-)
Yes, your code is definitely correct. Save it, restart the IDE, and wait to load all of its components. Then again run it!

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

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.

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.

main method with class name and file name

My file name is Temp.java and inside it I have this. I'm using eclipse IDE
/*package*/ class Test {
public static void main(String args[]) {
System.out.println("test");
}
}
So I was unable to run this as java application. I change my class name to Temp
class Temp {
....
}
Now I can. Can someone explain me why ?
This is probably a limitation of Eclipse. The code runs well from command line.
As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.
An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest
The code below, located in Temp.java, compiles and runs fine with Netbeans:
class Whatever {
public static void main(String[] args) {
System.out.println("hello");
}
}
The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public.
But you can still run the class using Alt+Shift+X,J.
Its not the problem with Java, its with Eclipse.
The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.
after .class file is created , run it with java Test
so here's an example
//Filename abc.java
public class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
the output
abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
^
1 error
but if you do this
//Filename abc.java
class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
it will create hi.class file so
D:\>java hi
Hell
The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.

Categories

Resources