Dia S
Dia S

Reputation: 31

Debugging code from specific class in java

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?

Upvotes: 2

Views: 3331

Answers (3)

Hearen
Hearen

Reputation: 7828

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.

Upvotes: 1

ultimate
ultimate

Reputation: 723

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.

Upvotes: 4

Alexander Ozertsov
Alexander Ozertsov

Reputation: 113

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

Upvotes: -2

Related Questions