2

Is there any way to debug only specific code or method of one of the class in an application without executing entire code from main method. One way would be writing junit for that class and debugging it with break point.Is there any there way?

Isaac Abramowitz
  • 542
  • 5
  • 17
Dia S
  • 31
  • 2

3 Answers3

4

You can have multiple main methods in your project - one in every class. If you wan't to debug some code in a specific class only, you can add a main method to that class and execute only the desired code. For launching your program, you can use your "main main method" again.

Note: in fact this is kind of similar to a JUnit test, except that you control everything and are not bound to the JUnit structure/syntax/etc.

Note 2: maybe you need to do some setup like in JUnit, too, if your class to be debugged relies on other parts of the project.

ultimate
  • 653
  • 5
  • 18
1

You can run the HelloWorld.java in any package as you want as long as there is an entry, which is the public static void main(String[] args):

public class HelloWorld {
    public static void main(String... args) {
        System.out.println("Hello world");
    }
}

By the way, normally we will have a main for each class for unit test as you mentioned, which provides us more power to do the job without any dependency and side effects.

Hearen
  • 7,420
  • 4
  • 53
  • 63
  • In my application there is only one main class and many packages, independent classes and methods which are called internally and I want to debug only those newly created/updated methods instead of running whole application. – Dia S Jul 31 '18 at 18:23
  • @DiaS Using maven or something similar? How about creating an entry and put your newly added methods there and right click that entry file and select the debug item? – Hearen Jul 31 '18 at 23:11
  • Creating entry point will be like adding main method in that class right. – Dia S Aug 01 '18 at 16:52
  • That is not intended.I just wanted to check the flow of my new code.So it is better to write Junit and execute n debug mode. – Dia S Aug 01 '18 at 16:53
-2

There is no way to debug your class without entrypoint (junit also have entrypoint). Java entrypoint is method Main.

  • 3
    Not Always. You can set an static method and run only the class with a static method. – Gatusko Jul 31 '18 at 15:10
  • @Gatusko Are you sure about that? Can you do that from the command line or is it a feature provided by your IDE? (which could in this case create a class with a main method on the fly) – Arnaud Denoyelle Jul 31 '18 at 15:15
  • 1
    I'll interject that it was possible with Java 6 but not anymore [See this answer](https://stackoverflow.com/questions/17889220/will-a-static-block-execute-without-the-main-method) – gtgaxiola Jul 31 '18 at 15:17
  • @Gatusko How could you run static method without any entrypoint?? – Alexander Ozertsov Jul 31 '18 at 16:24
  • @AlexanderOzertsov as metioned before. There a lot of ways in Java 6. In Java 8 you can do workarounds. Like adding as JavaFX Application, just try to do a search for it and you will see there are people that do it. But you can do it. – Gatusko Jul 31 '18 at 16:27
  • @Gatusko You are confused with IDE support and real life. In real life, there is no "JavaFX Application", this is just IDE feature for more comfortable working (like JUnit and Spring support). – Alexander Ozertsov Aug 01 '18 at 11:23
  • @AlexanderOzertsov OP Question is for debugging and nothing else and I never talked about IDE support. That is my last comment because this is going as off topic. – Gatusko Aug 01 '18 at 12:16