6

The problem: I am unable to debug a Maven project based on JavaFX 11 that is written and launched using Eclipse IDE 2019-03 (4.11.0) using the method suggested here and used in a related question.

Sample code:

public class HowdyJFX extends Application {

     @Override
     public void start(Stage primaryStage) throws Exception {
          final String javaVersion = System.getProperty("java.version");
          final String javafxVersion = System.getProperty("javafx.version");
          final Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
          final Scene scene = new Scene(new StackPane(l), 640, 480);
          primaryStage.setScene(scene);
          primaryStage.show();
      }

     public static void main(String[] args) {
         launch();
     }
}

If I set an Eclipse breakpoint at the first line of the start() method (where a variable is assigned), the application should stop running at that point, which does not happen; instead the application continues running as if the breakpoint wasn’t there.

A solution is suggested here and near the bottom of the question referenced above, but these both require a very different launch method than the one suggested by OpenFX.

Thanks in advance for any useful tips!

jfr
  • 397
  • 2
  • 17
  • JavaFX shouldn't affect the approach needed to debug your project. Since you're using Maven you should look into how to debug a Maven project using Eclipse. Specifically, assuming you're using `javafx-maven-plugin`, you should figure out how execute the `javafx:run` goal in debug mode and attach the Eclipse debugger to it. – Slaw Jun 11 '19 at 21:48
  • This [answer](https://stackoverflow.com/questions/56197372/i-cant-debug-an-application-using-netbeans-11-with-javafx-12/56207033#56207033) is for NetBeans, but the idea is the same. – José Pereda Jun 11 '19 at 21:55
  • @Slaw Thanks for replying. I am using the `javafx-maven-plugin` with the `javafx:run` goal. I have no idea whatsoever as to how to attach the Eclipse debugger to it. If you do, then please advise. FWIW, Under JavaFX 8 (running under JDK-10 or earlier), a JavaFX/Maven project written with the Eclipse IDE could be run (or debugged) with two button clicks. It's a different world now, I accept that. But integrating something as basic as debugging into the process should be 'out of the box' and handled by the jdk, or the plug-in, or Eclipse. It should NOT be the responsibility of the developer. – jfr Jun 11 '19 at 22:03
  • @jfr any luck on getting it working? – J_rite Jun 29 '20 at 08:35
  • @Jungkook No. Assuming that effective debug capability is important in the development process then one can only assume that OpenJFX discourages development of new controls or at least does not consider development of new controls to be important. – jfr Jun 30 '20 at 09:12

2 Answers2

3

I still don't get it why people are making things so complicated. The good old way to run or debug a program in Eclipse by just creating a run-configuration still works like a charm even with the latest JDK and JFX. (I use 15 EA for both). There are just two simple steps.

Add the following line of code at the end of your example file above. This will allow you to completely ignore all module system nonsense.

...

class HowdyJFXLauncher {public static void main(String[] args) {HowdyJFX.main(args);}}

Select the class in the Eclipse package explorer and right click on it. In the context menu select "debug as" and in the following option menu select the above introduced launcher and not the main program and then click ok. This will launch the program in the debugger as usual. This works for Maven and Non-Maven programs without any plugins or other stuff. If you have a module-info.java, remove it. You may have to rebuild your project in this case, so that the classpath gets setup correctly.

I work this way on a daily basis and are always wondering why people are making things so complicated.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Agreed, right-clicking the [app class]->Debug as -> [select option] is tried, true and simple. This works for pre-module jdks, but for later jdks definitely even where no module-info.java file is used. Your solution would be perfect ... except that the newly specified launcher doesn't appear in the context menu. As per your instructions I added the 'launcher' code at the end of the file thereby creating a new class. Are additional steps necessary to create a launch configuration that works? If so then please elaborate. Thanks – jfr Jun 30 '20 at 14:08
  • As invariably happens an idea came to mind immediately after sending my reply to your suggestion. I created a 'Run configuration' that specifies the launcher i/p/o the main class, and that works. However, b/c of this issue (which hopefully is now resolved) and others that have manifested themselves only after introduction of the module system I've been working with jdk 10.0.2. I'm trying to finish a (very) large library of custom JavaFX controls and need a jdk that works, with the intention of moving everything to a jdk 11+ environment when the library is finished.... [to be cont] – jfr Jun 30 '20 at 14:21
  • ...[continuation] I mention this b/c I won't be able to test the solution for at least a week or so, but will certainly provide feedback. On another issue that has come up only after the introduction of java modules (and mostly b/c of the side effects), would you have any thoughts about the issue raised here: ? Thanks – jfr Jun 30 '20 at 14:45
  • No, sorry. I don't use SceneBuilder anymore and this is once again one of these module system pain points which I'd like to avoid as much as possible. – mipa Jun 30 '20 at 15:19
  • thanks for the feedback, just a couple more quick questions and I'll stop bothering you: 1) did you mean that SceneBuilder is another module system pain point? and 2) what do you use an an alternative to SceneBuilder? – jfr Jun 30 '20 at 16:02
  • 1) No, it suffers from it as your other problem shows. 2) I use MigPane and code my views manually. You have to get used to it but in the end the results look better, its easier and the performance is better. – mipa Jun 30 '20 at 16:21
  • I tested your solution on a 'bare bones' app using jdk 14 and jfx 15EA and it worked in both (i) debug and (ii) run modes. Earlier test last week was with jdk 10 and its out-of-the-box JavaFX api. Interestingly, this is soooo much simpler than the approach suggested by the openjfx site [https://openjfx.io/openjfx-docs/#maven]. This all involves several projects that I think may interest you. If you could leave an email address in a reply comment (or open a chat) I could provide more details. If it's interesting - fine - and if not - that's okay too. – jfr Jul 08 '20 at 12:00
  • I communicated this issue on the javafx developers mailing list but I did not find an open ear there. https://mail.openjdk.java.net/pipermail/openjfx-dev/2020-April/025903.html (This also reveals my e-mail adress) – mipa Jul 08 '20 at 13:00
0

You can debug a javafx application by adding the following to your pom.xml file

<plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <executions>
                <execution>
                    <!-- Default configuration for running -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>be.canguru.Main</mainClass>
                    </configuration>
                </execution>
                <execution>
                    <!-- Configuration for debugging -->
                    <id>debug</id>
                    <configuration>
                        <options>
                            <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:8001</option>
                        </options>
                        <mainClass>be.canguru.Main</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>

After this run a maven run configuration with goal

clean javafx:run

enter image description here

After this you should see

Listening for transport dt_socket at address: 8001

in the console

After this you need to start the debug on port 8001 enter image description here

This should start your application and stop at the debug breakpoints

J_rite
  • 572
  • 1
  • 12
  • 28
  • Thanks for your input, it looks interesting. Most important is that (assuming it all works :)) you're using an Eclipse IDE. That is essential, a NetBeans solution is a non-starter. I won't be able to test your solution for at least a week or so b/c of other priorities, but when I do I will certainly provide feedback. Curiously, the Eclipse debug process for 'pre-module' jdks is straight-forward and simple, but for the 'new and improved' jdks that are several years in service it's a nightmare. Hopefully that will change. – jfr Jun 30 '20 at 10:57
  • 1
    What Eclipse function was used to bring up the window where we connect port 8001? When I run the run@debug, I don't get the "listening..." message, and instead get an error on the console: `ERROR: transport error 202: connect failed: Connection refused ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [./open/src/jdk.jdwp.agent/share/native/libjdwp/debugInit.c:734]` – Phil Freihofner Aug 25 '22 at 21:15