Real time code compiling? - java

I was watching the livestream at http://www.humblebundle.com, and I saw them running the application, AND making changes to the code in Eclipse. Is this just changes for the next time they compile and run, or was he actually changing the application as it was running somehow?

Sometimes.
In Debug mode, eclipse can compile and change class files in a running JVM, this is called Hot Code Replace.
The idea is that you can start a debugging session on a given runtime
workbench and change a Java file in your development workbench, and
the debugger will replace the code in the receiving VM while it is
running. No restart is required, hence the reference to "hot".
Source: Eclipse Wiki
There are certain circumstances where this won't work, in which case Eclipse will prompt you to restart.

This is called hot code replace:
The idea is that you can start a debugging session on a given runtime workbench and change a Java file in your development workbench, and the debugger will replace the code in the receiving VM while it is running. No restart is required, hence the reference to "hot".

It's not really possible to tell from the stream. Java's capable of both to some extent - you can, with some restrictions, replace classes loaded by a JVM using the debugger. There's also JRebel, which gets rid of a great deal of those restrictions.

Ability to change application code while running is the feature of debugger. In Eclipse and many other popular IDEs it works "out of the box". Feature has several restrictions: can't change method signatures, add/remove class members, etc.

If you are in debug mode you can make certain changes while the application is running. Whether or not it is valid varies based on if the change is to currently loaded code. For example, you could change a sort function while the program isn't sorting and it will use the new code the next time it sorts.

Related

What is JFR parameter filename meaning? File ends up being empty after exit

I have situation where I start JDK18 jvm from c++ code to produce vst plugin goal being to implement audio signal algorithms in java side with added value of full java GUI api. My framework works very smoothly apart from the repeatable state where my audio streaming crashes after 14 hours. So I thought this is good place to start learning JFR. My jvm starting parameters are in xml file and relevant part is:
<param>
-XX:StartFlightRecording,dumponexit=true,filename=c:/out/blackbox.jfr
</param>
Even when application exits that named file keeps empty. So what is the idea of filename parameter if it stays empty and how to use it?
The recording is dumped in a Java shutdown hook. If you terminate the C++ application with exit(status), the Java hook never gets a chance to run.
Not sure how to best run the shutdown hooks, but you could perhaps invoke System.exit(status) from native using CallStaticVoidMethod?
My solution with JDK 18 and flight recorder is not to use JVM startup options at all but instead use jcmd's JFR commands. This is due to incompatible JVM options at startup and lacking documentation. Available documentation is clearly for some older versions of JVM. Here is the available documentation:https://docs.oracle.com/javacomponents/jmc-5-5/jfr-command-reference/toc.htm which proposes use of -XX:+UnlockCommercialFeatures which has been long gone. What is current state of command line options is not achieveable for average programmer.
But "jcmd JFR.start" is example of things that work. I got things working observing with "jcmd PID JFR.check" . It is obvious that JFR api is also little bit broken and needs to addressed in a certain way to get the wanted results. There must have been very hurry when implementing it because the order of parameters is very crucial. And there is a nag that "name" must not be a number even it uses it as number. Now I know it is sensitive. So the way I want it to function is to sample and dump periodic chunks so that differences reveal them selves. Now I have the solution to that but it needs another question with no stupid complaints. Baseline is that jcmd with JFR parameter must be used as it comes out of the box in the way which is not obvious.

Multi-threaded time-based call hierarchy

I am using eclipse to write java code. If I'm debugging some code I can set a breakpoint and follow along as the code goes through each of the functions or I can backtrack. I can also look at the call hierarchy or the references to get an idea. But that's not enough.
I would like to have a some sort of time-based visualization of what each thread is doing along the process from ... let's say "point A" (pressing a button on the interface) to "point B" (getting the result). I want to see which classes/methods were called in what order. I want a good way to visualize what kind of output is coming from one method and going into another method which fires off a new process ...etc.
Is a profiler the only thing available for this type of visualization? Basically I want an action diagram or flow diagram created. Is there some plugin or app which can generate something like this?
Edit: Here is an example of what I'm thinking ... at least visually:
essmodel.sourceforge.net/index.html
It has some flow of where the code is leading. But I think this is just a static map of what classes lead to other classes and what inputs/output options are available. I would want to map the flow based on a specific case.
JProfiler offers such a view, it's called the "Call tracer":
It's important to restrict your filters very carefully in order not to record to much data.
Disclaimer: My company develops JProfiler.
I believe using a profiler is going to be your best option. Are you familiar with VisualVM? It comes with the JDK (look for "jvisualvm.exe" inside your JDK's bin directory) and is capable of profiling local virtual machines automatically as well as remote machines when configured properly. And it does give a pretty slick overview of what threads are running and the code they are spending time in, so I think you could easily do what you need from it. And best of all, it's free :)
As I said, local profiling is a breeze. You just run JVisualVM.exe standalone, and it will find any and all java processes running on the local machine automatically (you can just pick them out of a menu that VisualVM gives you upfront). If you want to profile remotely, set the following VM arguments for whatever it is that you're running:
-Dcom.sun.management.jmxremote.port=[0-65535]
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
Then within VisualVM, use the hostname of the machine your remote JVM is running on and the port you configured in the first VM argument above.

How to prevent NoClassDefFoundError when Java runtime is on a removable drive?

I have a Java application on a USB stick along with the JRE (one for each Windows, Mac OSX and Linux) so that it can be run on any system even when Java is not installed. I have a thread running to detect if the USB stick is removed and if it is it exits the application.
I want to have it display a message before exiting (or tell them to re-insert the drive) but if I try to dispaly a JOptionPane I get the NoClassDefFoundError because the JRE has been removed along with the USB.
Is there a way to keep the needed classes in memory so that Java does not try to load them from the filesystem that is no longer present?
Thanks!
For most runtimes, any classes that are loaded will be in RAM, in the PermGen section of Java's memory.
To my mind what's likely happening is that displaying the error requires classes that haven't been loaded before - which are only available on the drive that's no longer there.
You might be able to work around this by triggering your error displaying logic when the program starts up, except have it in a mode where it's displayed invisibly. This should exercise the same code path such that all the required classes are loaded, and ensure that they're already known when you need to display your error. If this is too difficult, you could just manually call Class.forName("javax.swing.JOptionPane") instead (for each class required), though this is more brittle and likely to break if you change your rendering code without updating the hard-coded classes to load.
If this isn't the case (i.e. all the required classes have previously been loaded, and you still have the problem) then your runtime is obviously unloading classes. You'd have to look at its documentation to see what it's doing here, and how to stop it.

Execution halts on "Microsoft Visual C++ Runtime Error"-popup

A project I am currently involved in uses JavaCv/OpenCv for face detection. Since the OpenCv occasionally throws an error, and the propagation of OpenCv/C++ errors to Java Exceptions isn't fully functional yet, this means the Java main-loop crashes with no way to recover.
However, the code gives mostly accurate results, and since we're running it on a large database I baked a quick Batch-script around the execution to keep it going, and the Java code internally manages an id, to make sure it continues from just after where it crashed.
:RETRY
java -Xmx1024m -jar Main.jar
IF ERRORLEVEL 1 GOTO RETRY
EXIT 0
However, occasionally I get a Runtime Error pop-up, as follows:
Microsoft Visual C++ Runtime Library
Runtime Error!
Program: C:\Windows\System32\java.exe
This application has requested the runtime to end in an unusual way.
Please contact the application's support team for more information.
At which point the code execution halts until the pop-up is clicked, which is really annoying, as it means my code can't run without me babysitting it.
I found this question, which basically asks the same thing. There is an accepted solution for that question, but since I'm not directly working with C++, I don't see how I can implement this.
Is there a Batch-level solution to this problem? Is there a Java/JavaCv-level solution to catch the C++ errors coming from OpenCv? Any other solution?
Interesting question.
Java.exe is dependent on one or more of Visual C++ DLLs (like MSVCRT.DLL, msvcr90.dll etc). Probably the JAR file is causing Java.exe to cause this error. Java.exe must be calling some CRT function which is raising the exception and hence the Runtime Error.
The best bet you can do is to launch the process, let this error pop and then start Process Explorer, and see the call stack. Nevertheless, solving this issue is most probably out of your control. May be latest version of Java may help.

Debugging a Multithreaded Webserver in JVM

I'm developing a multithreaded webserver applet and have been dealing with system.out.println's for the past week or so as my "debugging" tool. As far as I understand it, the .jar I build is split by a builder and put into .html files and then spit out when I access the web server.
I am wondering if there is some way that I would be able to attach a debugger (plugin?) to Java's virtual machine that would allow me to step through the code as I operate the webserver- there are some critical exceptions that are very frustrating to track down. I believe they're timing issues related to the multithreading which makes them even more unreliable when attempting to locate, and may mean that the debugger won't process appropriately. I don't think it matters, but the IDE I'm building in is Netbeans.
I've taken over a previous developer's hastily-not-quite-finished project and am in well over my head. =/ Thanks in advance for any possible solutions, I appreciate it.
Sure you can.
You need to start the web server JVM with suitable arguments allowing it to be debugged remotely. You can see at http://download.oracle.com/javase/1.3/docs/tooldocs/win32/jdb.html how to do it. jdb is available in the JDK along with javac. You then launch your IDE debugger to connect to your JVM and tell it where the source for your classes is located. Remember to compile with debug information.
As you have it inside Netbeans already then consider just launching it in debug mode as then all the extra work described above will be done automatically for you.

Categories