Simple Java alert for debugging purposes - java

I'm trying to debug/trace how some Java code is executed (or if it even is). I would like a simple way to do an alert just so I can tell how the code is being executed. Something like:
alert ("do we get here");
in javascript, or:
echo ("do we get here");
in php.
I have googled it but alot of the methods seem fairly complicated and an excessive amount of code. I am new to java, is there any way to achieve this?

JOptionPane.showMessageDialog(null, "Your Message");
The null part can actually be switched by a proper parent component, if there is one.

You can use
System.out.println("Here we are");
but if you want to know how we got there you can use
new Throwable("Here we are").printStackTrace(System.out);
and it will print the call stacka s well.

Sytem.out.println("do we get here");

If you just want to do text output to the screen, you could just use System.out.println("Point 1");. If you are after a more visual thing (which I am assuming you are), you could take a look at the JOptionPane class, with more information available here.
A more traditional (an most likely preferred) way of doing something like what you are after would be use Loggin. SLF4J is a relatively popular way of doing logging in a Java application.

Related

Java function on following function

Yesterday I made this question: Java function on function
For help and get marked as Duplicate but I think I didn't get understand there what I want and now I try again.
I want methods can be only called on methods for example we have the class Roads and on the road we will go a Way.
Roads.Way1()
After we choose the Way1 we will go to Path1
Roads.Way1().Path1()
But if we choose Way2
Roads.Way2()
We are not able to go to Path1() cause Way2() goes to Garden1() so
Roads.Way2().Garden1()
So what I try to say you can only use the methods(functions) in a wanted way and I saw this on different API or Library. So for the good understanding
Way1 goes to Path1 and ISN't able to go to Garden1
Way2 goes to Garden1 and ISN't able to go to Path1
So how to manager that I can make different roads that has there own ways so I could make like
Roads.Way1().
/*
Goes to:
Path1()
Fountain()
Market()
*/
And Way to cant access them and can only use there own destinations.
I think what you are asking for is: how can I express "control flow" using "language features". And well, there would be ways to get there: you would need a lot of different classes (or maybe interfaces); so something that is a "Way2" would simply not offer a "Path1" method.
But: doing so sounds like bad idea. It will work fine initially, but as soon as you start extending your system, you will be running into problems all the time:
"Hmm, I need to change Way2; it should now allow to go Path1; but uups; there are some Way2-thingies that should not allow Path1; so I actually need a Way3-thingy" and so on. Chances are extremely high that maintaining this code will turn into a nightmare very soon.
I know, this is just an opinion, but my gut feeling is: you are looking for the wrong solution to your problem. Instead, you should spent time on identifying what your actual problem is; and then think about better ways to solve that problem!

Clear The Console Programmatically In Java

I created a sample Java application. I want to clear the window options, i.e.:
Register
Login
Clear
If the user presses 3 I need to programmatically clear all options. Something like Console.clear?
Is there any way that I can do this with Java?
You will need to output a bunch of blank lines. Even in Windows/*nix, clear/cls doesn't truly clear the screen, it just prints enough blank lines that you cannot see the previous text.
You can try System.out.print("CLS");
Or use loops to clear the screen like
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
and then call this method clearScreen(); if you want to clear.
Sorry my english is bad. :) I just want to help you.
You mean you created a console application and want to clear the console (not necessarily the Eclipse console)?
If so then I guess you're looking for:
Runtime.getRuntime().exec("cls");
But be aware this will be system dependent.
If you are working with the console, then these might prove useful.
Using "backspace" character.
Using process builder.

Getting back to the console in Java

I'm writing a Java class which plugs into a larger framework. Somewhere upstream, the code is redirecting System.out to somewhere other than standard out, but I really want to print a debugging message to standard out. Is there a way for me to get a hook back into standard out?
Perhaps an easier solution is to use System.err instead of System.out for debugging messages? It would be a lot easier and would not introduce any side-effects of that redirection.
Why not use System.err?
The most straightforward solution I see is just store System.out somewhere in your class before calling the library.
After looking around a bit, you should be able to use System.console().printf(...).
You can simply write:
System.setOut(System.out);

Junit and messageboxes

I am new to JUnit and I got a sample java project in which I need to write unit tests for all the methods.
Unfortunately the code is poorly designed and some of the methods are done from the UI. Furthermore, some of the methods pop up a messagebox and do not return a return value.
I have two questions: First, without modifying the existing code, is there a way I can suppress the message boxes and not press enter every time I run the unit tests?
Second question: can a test function expect a message box and assert failure\success upon it's string content?
I appreciate any help, I know the best solution is to fix the code itself - separate the BusinessLogic completely from the UI and to test expected result, or even if message boxes are somehow mandatory use modal message boxes (like humble dialog boxes) but unfortunately I am not allowed to change anything in the code.
Thanks :)
Nili
There are all sorts of ways you could get started if only you were allowed to edit the code, so my first approach would be to see if you can get this restriction relaxed, and to read Working Effectively With Legacy Code.
Failing that you could try using a GUI testing framework like FEST-Swing to check the contents of the message boxes are as expected.
Not allowed to change the code, you say? First thought it to have a look at JMockit which really opens up a lot of possibilities when you are severely constrained by code that was not written with much concern about how it should be tested. It should enable you to, without modifying any code, substitute your preferred implementation of bothersome parts while your test is running--so only in the context of testing would you have altered the test subject (be careful to write a meaningful test!) or its dependencies. Other mock object frameworks can be useful, too, but the investment to learn JMockit is really time well-spent.
unfortunately I am not allowed to change anything in the code.
There's all sorts of stuff on Google about how to automate Swing testing with JUnit. Unfortunately, there's no way to get around this problem when testing.

Hooking an existing method in Java

I want to hook the method System.out.print in Java and have the ability to read/change the variables used in the method before the part of the method is called that actually adds the string to whatever the output stream is.
In C++ I would just detour the function, or set an int3 instruction so I could access the registers but in java I have no idea how to accomplish something similar.
You can rewrite the byte code of the methods, and in the process capture/change the local variables. It is not trivial. See some notes here.
Maybe what you really want is a java debugger? You can connect a debugger to a remote process, add a breakpoint, and capture/change the local variables pretty easily using eclipse.
What is the real problem you are trying to solve?
Have a look at this link.
He sneakily defines a static anonymous class so that System.out points to something different, and therefore print and println will route through that object.
You can reassign System.out (and System.err) to another object which does what you want to do with it. Said object usually gets the old System.out value so that output can be made in the end.
This is usually done in main() and influences the whole JVM.
We use this to have automatic wrapping at 130 columns in a very peculiar setting where longer lines are truncated.
Since JDK 1.1, the System.setOut and System.setErr methods are added to enable applications to hook the streams.
Link : http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut(java.io.PrintStream)
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setErr(java.io.PrintStream)
#Nowayz Some time before i too had the same problem with me.
After some research i came to know About AOP. AOP i.e. AspectJ provides a facility to intercept the java APIs by applying the pointcuts before,after, around. So have a look at it .You can refer my question on stack .it may help you.

Categories