I have downloaded and installed the processing IDE from their official website, then I followed the instructions on how to import the libraries into eclipse. I have core.jar, gluegen-rt.jar and jogl-all.jar added to path. I have the code below which I'm trying to run but when I go to the run menu there is no run options whatsoever. I only see 'run configurations'. Is there anything I'm missing?
package week2;
import processing.core.*;
public class ProcessingTest extends PApplet{
private String URL = "https://c2.staticflickr.com/6/5254/5428199232_c3678ed2ac.jpg";
private PImage backgroundImg;
public void setup()
{
size(800, 800);
backgroundImg = loadImage(URL, "jpg");
backgroundImg.resize(800, 800);
background(backgroundImg);
}
public void draw()
{
}
//public static void main(String args[]){
// PApplet.main(new String[]{"--present", "week2.ProcessingTest"});
//}
}
As of Processing 3, PApplet no longer extends Applet (more info here). In other words, you can't run sketches as an applet anymore.
You'll have to put your main method back in and run it as an application instead.
If you really want to run as an applet, you'll have to create your own class that extends Applet or JApplet, and then add the Processing component to that. That can be pretty convoluted, plus applets are pretty much dead now anyway, so you're probably much better of deploying as an application- or even better, as JavaScript using Processing.js.
As a side note, you shouldn't have to rely on eclipse automatically detecting the run configuration. You should be able to go into the run configurations and create one yourself. That won't work for this specific case, but it's not a bad idea to get more comfortable with the "behind the scenes" stuff so cases like this are less confusing.
Related
I'm learning java and practicing on sololearn.com and I copied one of the examples to practice typing code. However the code here -
//Create myClass
class Loader extends Thread {
public static void main(String[] args) {
}
public void run(){
System.out.println("Hello Young World");
}
}
public class MyClass {
public static void main(String[] args) {
Loader obj = new Loader();
obj.start();
}
}
isn't printing "Hello Young World" to my console. In fact I had to add a 'main()' method to the Loader class just to run MyClass.java. However in the example their code ran without having to include a main method in Loader. Maybe they have customized their environment to allow for this type coding and IntelliJ just has different rules. Please could someone copy the code on their machines and run it with IntelliJ to see if they run into the same problem?
I've troubleshooted, but the code seems to be solid.
I guess you start the wrong main-method (the main from the Loader class which is empty). As you noted, you don´t have to add a main-method in your Loader class. Please remove the method and start the main-method from MyClass.
If you have several main-methods, you can choose which one should be executed in the run configurations. In your example, it should look like this:
In this case, make sure you select the one you like to execute.
Another way to execute the right main-method, is to select the class which contains the main-method and hit the play button on the left side:
To run the program in my console I had to 'java MyClass' rather than running 'java MyClass.java' which executed the whole file rather than just the class whose main method I wanted to call!
To run the program in IntelliJ I had to make sure that the right Class was being called as pointed out above.
Yesterday I uploaded some project to my github It compilled fine, Today I'm trying to clone it from other computer, I followoed this guide, but when I'm trying to run the code I'm getting:
Error: Could not find or load main class gui.MainScreen
Caused by: java.lang.ClassNotFoundException: gui.MainScreen
I tried to do like this post, but I didn't understand his answer, can someone please post clearer answer? Screenshots will help a lot.
Code roughly as follows:
package gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainScreen extends JFrame
{
public MainScreen() throws IOException
{
....
this.pack();
this.setVisible(true);
}
}
public static void main(String[] args)
{
//avoid blocking the main thread
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
try {
new MainScreen();
} catch (IOException e) {
e.printStackTrace();
};
}
});
}
}
You need to have the main class in a separate file. From your screenshot, it looks like you have the main class within the MainScreen class. You can name the file that has the main class anything you want (don't name it main though, just to avoid confusion).
If your main class is indeed in a separate file, set the main class after cloning your project in eclipse. Check this link How to set the main class in Eclipse. I am not sure that information is present in the github project that you cloned. Go to Run Configurations and setup the fully qualified main class name and as the link says, you can also search the whole project, which will give you the main class. Set that and then run the project. Hopefully, that will work.
I do not know if this answer will be beneficial to you or not but the same issue happened with me. To fix this what I did was just deleted the project from workspace and again imported it back from the git folder and everything started working fine.
My suggestion for those who encounter this problem, is to create a new draft project in Eclipse, see that it's running well, and then compare it to the
project > preference > java build path
that there is there (by edit you can see the settings of the working project vs the not working).
You can use screenshots to be sure.
When we are cloning a project it makes sense that some setting of the project are not fit to our local Eclipse setting, so we need to handle that.
I have a question.
How does one initialize the JavaFX toolkit with the method I found in an earlier StackOverflow Question? The topic can be found here: JavaFX 2.1: Toolkit not initialized
I am trying to use a solution similar to this solution from that thread:
Problem: Non-trivial Swing GUI application needs to run JavaFX components. Application's startup process initializes the GUI after starting up a dependent service layer.
Solutions
Subclass JavaFX Application class and run it in a separate thread e.g.:*
public class JavaFXInitializer extends Application {
#Override
public void start(Stage stage) throws Exception {
// JavaFX should be initialized
someGlobalVar.setInitialized(true);
}
}
The only problem I have is: What do I do with
someGlobalVar.setInitialized(true); ?
I don't know what to fill in there, and some tips would be appreciated :)
I am writing this answer for the comment you have done about how to support (.mp3,.wav,.flac,.ogg) etc in java.For .mp3 you can use JLayer http://www.javazoom.net/projects.html search on web for examples.
About (.mp3,.wav.flac,.ogg) and some more you can use JavaZoom BasicPlayer which uses some external libraries to support them you can download the zip folder here(download without installer and you open the zip folder).
Then go on the folder lib and copy all the .jars except kj_dsp which can be used for visual representation on audio data and contains also a class about fast fourier transform(FFT).Also change MP3_SPI1.9.3 with MP3SPI1.9.4
Then add these .jars into your project libraries and just use:
BasicPlayer player = new BasicPlayer();
The whole thing uses Service Provider Interface (SPI) mechanism.
It runs on a separate thread so you don't have to worry.It works really well but the project is a little bit old.It's a good start!About docs check the website.
import com.sun.javafx.application.PlatformImpl;
public class JavaFXInitializer
{
public JavaFXInitializer()
{
initFx();
}
private synchronized static void initFx() {
PlatformImpl.startup(() -> {
});
}
}
I have a launcher and a JavaFX class. The launcher creates a class called JavaFXApplication1. The JavaFXApplication contains the whole JavaFX code (Just a little example in this case) and should setup a window with one primary stage.
The launcher has the static main entry point - but I read that JavaFX doesn't really use this entry point. This explains my console output (See the end of the post)
I don't know if this is possible (Launcher create a JavaFX window - the entry point is not in the presentation class itself) . I don't want to use a preloader (I think preloaders are just for heavy loads during startup), because the launcher represents the whole program as one object (Presentation, business and persistence - a 3 layer program). The entry point should be outside the presentation class (in this example in the launcher class)
The following example does work. But for me it is like a piece of "black magic"
Here is my code
Launcher:
package javafxapplication1;
public class Launcher
{
public static void main(String[] args)
{
System.out.println("main()");
// Do some stuff and then create the UI class
JavaFXApplication1 client = new JavaFXApplication1();
client.caller(args);
}
}
JavaFXApplication1:
package javafxapplication1;
import javafx.application.Application;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application
{
#Override
public void start(Stage primaryStage)
{
System.out.println("start()");
primaryStage.setTitle("I am a JavaFX app");
primaryStage.show();
}
public void caller(String[] args)
{
System.out.println("caller()");
launch(args);
}
/* We call the main function from the client
public static void main(String[] args)
{
launch(args);
}*/
}
And the output for the program is:
start()
Is there a way to create such an application ? Thank you
The answer to this problem is to create a java project and not a JavaFX project. After this you can add a JavaFX main class and write a method (call launch() ).
Maybe you have to add the compile-time libraries deploy.jar, javaws.jar, jfxrt.jar and plugin.jar from the /jdk_*/jre/lib directory
I written a post at
Running JavaFX Application instance in the main method of a class – MacDevign
Could it be what you looking for ?
The code is quite long hence it is better to refer to the post however the usage is simple. Take note that the init and stop method do not use launcher thread so use it with care.
The purpose is to run a dummy javafx application on your main method of your class for quick testing/experimenting purpose.
To use this, just add the following in the main method using lambda, or alternatively you can use anonymous inner class style.
// using the start method of Application class
Utility.launchApp((app, stage) -> {
// javafx code
}, arArgs);
With Swing applications I can use an external class to instantiate and view them.
I'd like to do the same with an Applet, outside of Eclipse, without using the appletviewer.
I want to be able to Run a class MyappletRunner and have its main method kick off the following applet for viewing.
Given the following source code:
import java.applet.*;
import java.awt.*;
public class Myapplet extends Applet{
String str;
public void init(){
str = "This is my first applet";
}
public void paint(Graphics g){
g.drawString(str, 50,50);
}
}
There is an example shown on If I embed a third-party JApplet in a Swing GUI, how do I pass it parameters?.
The basic idea is to create your own Swing Frame, add the Applet to your frame, and then pass your Applet an instance of the AppletStub interface.
The best example of it appears to be here:
http://www.java2s.com/Code/JavaAPI/java.applet/implementsAppletStub.htm
Appletviewer is the tool you want (if you are just testing that is): http://download.oracle.com/javase/1.3/docs/tooldocs/win32/appletviewer.html
If you want to launch it as a standalone app, the following tutorial works: http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/
Not exactly sure why you are trying to do this... what exactly are you going to accomplish by doing this? You say you want to run a java applet... within a java application... AFAIK, it isn't possible anyway. If you just want to view the applet, use appletviewer.
write the folloing code in the begining of the xyz.java file:
/*<applet code="xyz.class" height=30 width=50></applet>*/
Save and compile the xyz.java file.
Now, Execute the file using the appletviewer as following:
c:\jdk\bin>appletviewer xyz.java