How much code is visible to JVM? - java

As every java application needs the main method except the case of applets and web application where the main method is implemented by web container so as the main method is starting point of the program as we pass the name of the class including main with java command to execute it.
So my question is how much code is visible to JVM means does it sees the whole code or it loads classes or information it came across during execution of the program.
Maybe my question is out of context, but please clear it.

Java loads classes "on demand". As soon as you somehow "use" X, X.class is loaded. Please note: "using" is more than just "importing" within source code.
You can find a nice introduction to this topic here.
And, as Holger points out correctly: you have to distinguish between loading and initialization time.

Related

How to check which classes act on an app feature?

Using android studio, if I run an app on an emulator, and I click on features, how can I know which classes in my code are currently acting?
Java is an Object Oriented Programming Language.. In simple terms, this means Java code is pretty much objects calling other objects to do stuff. Therefore, when an android app is being run, many classes are actually simultaneously being run/called (or as you put it: acting).
But there's still a simple hack you can use to know the classes being run at the moment (among the classes you wrote):
I'd suggest you log the entry point of every class you want to monitor. For example, let's assume you have an app with three classes, in the entry point of each of the classes (be it an onCreate(), a custom class constructor or method), you can simply just log so you can monitor in your logcat.
Practically, when you use the method above, you get logs every time a new class is currently on focus.
I hope this helps.. Merry coding!

Java noob, wondering if I made a mistake regarding packages

So to give context, I am new to Java and have no other programming experience. The IDE I am using is NetBeans. I picked up a book called "Sams Teach Yourself Java" and the tutorials in this book are having me put all the different classes I write in the same package. The problem with this is that when I want to run a class with attached arguments I can't just run the file from the "Run" tab. The book tells me to run it as a main project. But if I select run as main, one of my other classes runs. I figured out a work-around buy setting a main class, but I haven't found anything online about this and want to make sure I am not doing something stupid. This is my first question and any tips on how to ask effective questions on this site are appreciated.
While learning, there is nothing wrong with putting many classes in the same package. The reasons for separating classes into different packages can wait until later lessons and learning.
You can run a particular class as a Java application in Netbeans, assuming it has the correct main() method, by pressing ctrl-shift-f5 while that class is the one currently selected (i.e., currently displayed in the editor pane). You can run the most recently run java application by pressing ctrl-f5, even if that class is not the one currently selected.
This question is clear enough. If you get to a programming problem, go far enough to have tried something that doesn't work as expected. To ask about it, try to show the smallest program that illustrates your problem; tell WHAT is happening that you do not expect (or not happening that you do). If there is an error message, include all of it, don't just describe it.
Is each class a new project/example from the book?
You could have multiple classes within the same package that each have a main method. Only the main method within the class that is selected to run/passed on will be called.
In NetBeans, there is a little drop down arrow that you can press and it should allow you to specify which one to call.
The application is running properly. That I can assure you. Since all the classes you created contains main method the compiler is confused because it does not know which class's main method should be executed successfully. Hence, it builds all the methods but does not execute them. If you observe the output tab, after clicking on the run button, it will always show build successful. This means it it building the classes but not executing them. To execute each class separately, either right-click on the class and then select the run option, or use the keyboard shortcut 'Shift+F6'. This shortcut executes the class you are currently working in.

Refreshing Swing application with Eclipse/MyEclipse

Say that we are writing a Java Swing application and we use Eclipse or MyEclipse to develop it. In web applications, you make code changes, you save and your ant deployment file takes care of the deployment of the changed files. Then you just refresh or hard refresh the web page and the changes appear there. Can we do the same thing for a Swing applications so that we don't have to close and open the program from the beginning every time we make a change?
I don't think so because you need hot code replacement ! Maybee using another framework.
You can't simply do that because once JVM is started, it loads the class files once and will not reload it untill next loading request. But you can use ClassLoader to load modified class files dynamically.
The following two articles may help:
IBM article on "hot class swap"
"Who Said Runtime Class Reloading Is Hard in Java?"
The first one is in Chinese, but you can look at the code and the result. I think the second article is more helpful for a GUI application.
In MyEclipse you can start your application in debug mode instead of run mode and changes you make will be pushed to the target VM; if changes you make cannot be replaced you'll see a dialog informing you the replace failed and you will need to restart your application. You don't need to place any breakpoints in the application, just starting in debug mode is sufficient.
As Guillaume states above, changes to the class structure will typically not be hot-synched, but changes within existing methods should be fine.
Obviously, how successfully hot-synched changes affect your running application would depend on your application design.

Java Applet runs without a main method?

I was running a Java class that extends Applet implements Runnable and apparently the program can run, but there is no main method. I thought Java applications needs the main method as its entry point?
Java Applets have an init method instead of main.
It's:
public void init() {... }
Yes, but applets aren't applications. There is a main method in the applet runner (assuming it's implemented in Java; it need not be) but the applet doesn't work that way; it gets loaded/instantiated from a file and then it proceeds along its lifecycle through initialization, starting, operating, stopping, and finally being destroyed. The code that sends it through these states is hidden from the applet's view; it just knows its in an environment that can run applets.
Applets differ from stand-alone Java applications in that they do not need to implement a main method.
Life Cycle of an Applet
Copied from google results:
Applets are standalone programs which require a third party tool for its execution that is either it is java enabled web browser or applet runner. So it doesn't have main(). It is
possible to run a program without main.
Possible duplicate of:
Why do applets not need a main()?

Eclipse - showing full call stack (like when it hits breakpoint in debugger) without putting in breakpoints?

I'm working with a legacy Java app that is new to me so one way to figure out how it works and find things easier, I have thought would be to be able to get the full stack trace after I perform actions, so as to be able to see which classes are being used based on a particular UI action. I had thought this was possible in the debugger but it seems that it only works if I insert a breakpoint and in this case part of the purpose of this is so that I don't have to know what's being called to be able to insert the breakpoint first (as this would help tell me that).
I apologize if this is a basic question, I have searched on this but I'm not finding the correct answer.
This doesn't directly answer your question, but maybe it will solve your problem better. Take a look at BTrace. It lets you instrument a running Java app and insert some basic code of your own. You could, for instance, have it write out entire method call chains to help you find your way through the app. It's somewhat similar to AspectJ, but with an entirely different purpose and requiring no change in the project source:
"BTrace is a safe, dynamic tracing tool for Java. BTrace works by dynamically (bytecode) instrumenting classes of a running Java program. BTrace inserts tracing actions into the classes of a running Java program and hotswaps the traced program classes."
A few suggestions:
Some profilers will allow you to walk from any particular method up (and sometimes down) to see what's calling and being called. I've found this surprising informative about flow, even in apps I thought I knew well.
For understanding the mainline flow, I don't think there's a better substitute for working interactively with a debugger. It will lead you into learning other important things. Not what you wanted to hear, I know. This presumes that you can rapidly restart the app when you miss a key off-ramp.
Reverse-designing large legacy apps is the one place where I use UML fairly regularly. There's too much to keep in my head to form a good big picture. If you have a UML tool that will do reverse-engineering, load it up with the app, then probably prune down hard on the classes you don't care about, because they are trivial or obvious. Arrange the diagrams in a way that helps you understand. I've used Together, Magic Draw, and Visual Paradigm in this way. Together worked the best - but it was a decade ago.
When you are in the debugger perspective, you will see a view showing the launched processes. In that view you can tell it to pause all threads of a process. Once stopped, you will be able to browse through threads to see what they are all doing. To try to catch what a particular action is doing, you would have to start the action and then quickly pause all threads.
You could always run the application with the VM arg of -verbose:class. You could then watch the console output and see what classes the VM is loading when you perform a particular action. This could possibly give you a starting place for where to place breakpoints. This won't always work depending on the scenario, but may be helpful.
Another trick you can use is to figure what classes you know that have to be involved in the code path you are trying to trap. For instance, you mentioned that it's a Java EE web app and therefore the action is likely some kind of a servlet interaction (at some level). I don't have the API in front of me, but you can place a breakpoint on the method in the response object where the output stream is retrieved. Once that breaks, you will know the code that's trying to service the request.
You can always see where a method is called by clicking "Open Call Hierarchy" from eclipse (left click on the selected method or CTRL+ALT+H ). Also, you always can inspect where a method/class is defined by clicking "Open Declaration" (left click on the selected method/class or F3).

Categories