I have just started using Javafx and I have enjoyed it so far. However I wanted to do some de-bugging with it, and I get this error.
Error: Main method not found in class fx.MyProgram, please define the main method as:
public static void main(String[] args)
If I go ahead and add a main method... the body is empty because all the magic happens from button clicks from the View. Do I need to change some configurations? Running the app works fine, but de-bugging doesn't work.
nvm I figured it out myself. For anyone else who runs into this...
public static void main(String [] args){
Application.launch(args);
}
Related
Is there any equivalent of this JavaFX Preloader and Netbeans, for the IntelliJ editor either online that I'm not finding or in someones noggin?
I've followed these posts
How to create splash screen as a Preloader in JavaFX standalone application?,
Java 9 JavaFX Preloader but I'm still a little lost and unsure - I end up with 3 main functions (1 for Main containing code below, 1 for TestPreLoader extending PreLoader and 1 for MyApplication extending Application) and Application.launch in
public class Main {
public static void main(String[] args) {
System.setProperty("javafx.preloader", "TestPreLoader");
Application.launch("MyApplication", args);
}
}
will not accept args as a parameter even though the types are identical.
Also, I've tried this from jetbrains but it doens't tell me anything about preloaders or how to link it with the application or why it's important. https://www.jetbrains.com/idea/help/applications-with-a-preloader-project-organization-and-packaging.html
Anyone have any ideas? Thanks
So I figured it out, it was a little silly. Turns out I just needed to do this.
public class Main {
public static void main(String[] args) {
System.setProperty("javafx.preloader", TestPreLoader.class.getCanonicalName());
Application.launch(MyApplication.class, args);
}
}
how to fix this problem: Main method not found in class jamel.Jamel, please define the main method as:
public static void main(String[] args)
enter image description here
The main method is just a method that java calls that actually does stuff in your program. If you don't create this method then what is your program for? You'll just have a bunch of classes that you don't use.
If Jamel is your main class where your application starts from scratch then you need
public static void main(String[] args) {
}
this is the entrance of your application to start with.
from here on-words in your main method you should start calling other object.
but from the screenshot I assume that that is not your want. My guess may be wrong. but figure out what is your starting class and does it have a main method and main method calling right object to start your application.
class a {
public static void main(String args[]) {
}
}
i want to print "hello world" without touch or use above code and also without use of static and final keyword......
What are the different ways to initiate code execution.
Use public static void main(String args[]) { ... }
Use your static class initializer static { ... }. Problem: You need to get the class to load first. Solution: attempt to start the main method, but that means you need to have at least an empty main. However - OP states we cannot use 'static'.
Use an instance initializer { ... }. Problem: How do you instantiate an instance. Solution: initialize a static field with a class instance. However - OP states we cannot use static fields.
So what is left. I assume that we can use the empty main posted by the OP. From there on we build up a 4th solution:
public enum Play {
PLAY;
{ System.out.println("Hello World!"); }
public static void main(String args[]) { }
}
This should fulfill the conditions:
we do not modify the static main method, although we need the empty body just to get the class loaded.
we do not use a static initializer, we use an instance initializer instead.
to instantiate, and get the instance initializer started, we use an enum. Enum's start the initializer for each instance, meaning for each of the enum constants.
Also note the output when you try to execute a class without a main:
$ java -classpath . Play
Error: Main method not found in class Play, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
So that leaves us also the clue that we potentially can use javafx.application.Application. I did not investigate this trail any further.
You can create a JavaFX application, which is usually for displaying a GUI, and use it to output hello, world to the console and immediately shut the JavaFX GUI system down. JavaFX applications are executed by a special launch path in the java launcher. JavaFX applications do not need a main, static and final keywords.
The following snippet is just for demo purposes, I wouldn't recommend writing a JavaFX application unless you actually wish to display a GUI.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
public class PlayApp extends Application {
#Override
public void start(Stage stage) {
System.out.println("hello, world");
Platform.exit();
}
}
// Totally useless code from the original question
// goes below the real app code and is just ignored
// and never used when the application runs.
class a {
public static void main(String args[]) {
}
}
You need to use Oracle Java 8+ to compile and execute the JavaFX application as below:
javac PlayApp.java
java PlayApp
The a class is in the PlayApp.java file only to meet the original poster's requirement from a comment:
it means the above code is fixed, you can't modify the above code but yes u can write anything to print "hello world" above or belove this code
Really, if you are writing a console application, just use a standard app format (with a static main method) instead:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Unrelated advice on asking questions
In future, if you post such questions, I suggest that you also supply the reason for the question and why the bizarre constraints are placed on solutions. Also, address concerns such as WorldEnder's first comment on your question: "is this a code challenge? If not.. why?"
You can't just print "hello world" without MAIN method and also without using STATIC and FINAL key word in java because the main method is a function required you don't have to apply any rocket science in that thing if u are using intelliJ IDEA or any other IDE anyways this is the code
public class Basics_Of_Java {
public static void main(String[] args) {
System.out.println("Hello world");
basics of java in 1st line is the name of the java file you may have another name.
I want to create a simple java class, with a main method, but when I compile my code, I get this error message :
Error: Main method not found in class errors.TestErrors, please define
the main method as: public static void main(String[] args)
This is the source code :
package errors;
public class TestErrors {
public static void main(String[] args){
System.out.println("hello");
}
}
Why I'm seeing this error, as you can notice I've alreader declared the main method !
As said in my comments, looks like you've declared a String class among your own classes. To prove this, I've created a basic example:
class String {
}
public class CarelessMain {
public static void main(String[] args) {
System.out.println("won't get printed");
}
public static void main(java.lang.String[] args) {
System.out.println("worked");
}
}
If you execute this code, it will print "worked" in the console. If you comment the second main method, the application will throw an error with this message (similar for your environment):
Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:
public static void main(String[] args)
This usually happens if ur complete project isnotconfigured correctly
or one of your class in project has still some errors
in such cases IDE will prompt stating the same that project contains some error
and you still proceed (ie run your class)
as project has some bugs new classes will not be created and IDE will run
the class which was available previously
to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence
Just check your java file, it has not been saved. Please save all java files before compiling.
If the answers above are not working for you: make sure "main" is not capitalized in your method definition.
OK:
public static void main(String[] args)
ERROR:
public static void Main(String[] args)
Though the error message makes the required syntax for the main method clear, incorrect caps can be hard to spot. It took me ~30 minutes of troubleshooting to find this typo today. This was clearly not an issue for the OP, but is another easy/likely way to produce the same error message, and this answer may help other users.
I had this issue just now. This error came up because the JRE file that i switched out didn't had the full library. After I corrected/added the right JRE system library the problem went away.
Right click on the class name (which you are trying to run)->Run As->Run Configurations->Under Main Class Tab
Write your main class name and hit on Run.
Try commenting the first line.
//package errors;
I have more of a understanding problem.
I added a new class in my Android porject. The class is not an Activity class, it uses objects of also non-activity classes to get some information.
However, I can not test, if it does its job. I can not debug it, none of the Log- messages that I built in the code show up in logcat.
How can I test a class in a project otherwise?
I have pretty limited knowledge in java+Android+Eclipse, so I don't know, which one is the issue here.
Please help.
Create test Activity, add there some UI elements which do some logic with your non-Activity class, and test it
Add a class with
public static void main(String[] args)
and run it.
E.g.
public class Runner {
public static void main(String[] args) {
//do smth here
}
}
You could simply develop a basic Activity with buttons that trigger the functions of your class