This question already has answers here:
Can't run JavaFx code from Eclipse
(3 answers)
Closed 5 years ago.
I have created and complied several javaFX applications without main methods in NetBeans and InteliJ IDEA. I have also compiled successfully a simple JavaFX application (see the code below) without a main method in windows command line.
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloJavaFX extends Application{
#Override
public void start(Stage stage){
stage.setTitle("Hello JavaFX");
stage.show();
}
}
So I believe it is possible to run JavaFX applications without main methods in eclipse too. But when I try it, I fail and eclipse indicates could not find a main method. I have added JavaFX jar file to my eclipse IDE and I can compile and run JavaFX applications with main methods in eclipse. So I guess I should add some path or address to help eclipse to run it, but I don't know how.
My question is "what else should I do to make eclipse compile and run JavaFX applications without main method?"
Actually reading the question " Can't run JavaFx code from Eclipse" and its answers helped me to figure out how to run a JavaFX application without main method.
First we add the main method and run it once as follows:
public static void main(String[] args) {
Application.launch(args);
}
Then we comment the main method out. Now the eclipse IDE run configuration is automatically set to run the application even without the main method.
Thanks #Oleg for directing me to the abovementioned question.
Now, if nobody has any other answer, I announce the question as duplicate.
Related
This question already has answers here:
How to run a java class with a jar in the classpath?
(4 answers)
What does "Could not find or load main class" mean?
(61 answers)
Closed 11 months ago.
I feel I need to declare that I am new to Java. I have read various Stack Overflow posts on how to call non-static methods in my main method. I believe the answers I have found are correct, but when attempting to compile and run my program, I keep running into the error:
"Error: Could not find or load main class HelloWorld Caused by:
java.lang.ClassNotFoundException: HelloWorld"
To explain a little more. I am completing a Coursera Java Specialization through Duke. However, I do not want to use the Blue J IDE and I am just running it with VIM instead (I just like the plain editor). I know it is easier to use IDEs, but I would rather know what I am doing (eventually) than be reliant on too many external programs.
The pre-configured BlueJ from Duke comes with the edu.duke libraries pre-configured, however, I have to manually do this myself. I have made use of the following commands when attempting to run the file:
javac -cp "./courserajava.jar" HelloWorld.java (No problems here).
java -cp "./courserajava.jar" HelloWorld (Error arises here).
I am new to creating instances of Objects for non-static methods, but from the other forums it appears as though what I have included below is correct.
The courserajava.jar file is located in the same directory as my HelloWorld.java file.
The file structure may be seen in the below screenshot:
Directory File structure
Then code for my HelloWorld.java file:
import edu.duke.*;
public class HelloWorld {
public static void main(String args[]) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.runHello();
}
public void runHello() {
FileResource res = new FileResource("hello_unicode.txt");
for (String line : res.lines()) {
System.out.println(line);
}
}
}
I promise I have read about 20 forums and cannot seem to get my file to run (I have other more "complicated" projects in java I actually need to run but I have not worked with compilers much hence I decided to just use this simple HelloWorld practice example).
I made a java application using javaFx on Eclipse IDE.
I am using JavaSE-11 compiler and the javafx-sdk-11 version.
It works when I run it from Eclipse, but now i'm trying to make a runnable .jar of the application.
When I double click on the .jar, nothing happens. I tried installing, uninstalling and reinstalling Java ...
Here are the issues I'm having when I try to launch it with [java -jar filename.jar] command :
Extracting the required libraries into generated JAR
java : Error : JavaFX runtime components are missing,
and are required to run this application
Packaging the required libraries into generated JAR
java : Graphics Device intialization failed for : d3d, sw
Error initializing QuantumRenderer : no suitable pipeline found
Don't hesitate to ask me more details, I'm not very good at this but I'm trying my best.
Thanks
I had same problem few days back. I just made another class that doesn't extend Application, and has its own main. Then just launched the application from that class like so:
public class Fakemain {
public static void main(String[] args) {
game.main(args); //this is the class that extends Application
}
}
Let me know if this worked for you.
I'm new to programming and don't know what to do... jGRASP gives this error (the title) when i try to run an mp3 file using java via this code :
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
public class hehe{
public static void main(String[]args){
String krow="hoho.mp3";
Media trial = new Media(krow);
MediaPlayer Ply = new MediaPlayer(trial);
Ply.play();
}
}
I searched for a solution but couldn't find one.
You need to download e(fx)clipse plugin or use NetBeans or IntelliJ. If you are using 'regular' Eclipse, you need to add jfxrt.jar to your classpath.
What you are using is JavaFX. It comes along with your JDK. However, jfxrt.jar is not on standard classpath.
In spite of resolving that, your program won't run because running JavaFX program is different from running 'usual' Java programs. You need to extend the Application class and create a scene graph.
Have a look here on how to get started.
I am using jdk 1.8 , jre 1.8 and eclipse juno.Whenever i use to run my program with eclipse it gives me this error
Error: Main method not found in class A, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
my basic program is
public class A {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Then i tried to run my program from CMD on the first time it run successfully after that it also start giving me this error..Points to remember
When i create a new project i use default jre.which is jre 8.
I am not using any kind of javaFx Application.My basic program is in front of you all
I cant every time extend javafx .So i need a strong way to resolve it.
I run a program from the command prompt for the first time it run ,but when i run another program with little bit of change it did not run.
I am stuck here guys please give me a solution for that and a little bit of explanation for that will be appreciated..
Please, close your cmd prompt.
And reopen cmd prompt.
Then open your directory containing pom.xml
To build your project, Type : mvn install
Then run the java code. For eg,
java -cp target/sample-0.0.1-SNAPSHOT.jar com.maven.sample.HelloMaven
Here HelloMaven is the class name.
I had the same error. Imported java.lang.String and the error went off.
import java.lang.String;
a few years back in college I wrote TONS of simple little java programs... I haven't used them in a while and I would like to go back and revist them. I believe I used Netbeans as my IDE (but i also could have used Eclipse... cant remember)..
anyways long story short.... I cant remember how to compile them and I cant open these as projects in any of my IDEs....
I've tried using javac and then java to run a few in the command line, however one specifically will not run and gives me this error:
Exception in thread "main" java.lang.NoSuchMethodError: main
I know this is because I don't have a public static void main method in this program. I can't remember why I don't.... but here's in theory how it should work.... I have 3 program files:
race.java
bicyclePanel.java
controlPanel.java
race extends JApplet and has a method public void init().... which creates an instance of controlPanel class.... controlPanel class creates an instance of bicyclePanel and so on and so forth....
my question is.. am I supposed to have a main method? How can I rebuild this into an IDE and run? It's been so many years I cant remember all the in's & outs of java :(
Yes, you are supposed to have the main method if you plan on running it as a standalone Java application. In Eclipse you can use the Applet runner to run your applets directy as well. It's Run As -> Java Applet.
Fire up NetBeans or Eclipse and Import the Projects with existing sources. It will do most of the stuff. You may have to adjust the broken references.