How can I find out the class containing the main() method inside the OpenJDK JVM (Hotspot) code? - jvm

In thread.cpp the method Threads::create_vm is defined which initializes the main thread and the VM thread. While I found this code location, I would like to know how the main thread knows which Java main() method to execute, i.e., in which class to look for, but I couldn't find that out.
As soon as possible after the VM creation (or even before), I would like to obtain the class name (and its package) of the class that contains the main method (and as a first step, just printf it). I thought about looking at the bottom-most entry in the stack frame of the main thread, yet the stack frame does not yet exist during Threads::create_vm. Can someone help me pointing me in the right direction?
tl;dr: I want to modify the OpenJDK source to print the class name of the class containing the Java main() method, how to do this?

When VM is created, it does not know what class/method will be executed in it. It is a job of a launcher to invoke main Java method using one of JNI functions. BTW, this method does not necessarily need to be called main.
I want to modify the OpenJDK source to print the class name of the
class containing the Java main() method, how to do this?
You probably want to modify Java launcher then. See java.c.

Related

Java method not available in Kotlin?

I am using the ejml-library (written in Java) in a Kotlin project. I imported the library (everything seems to work fine) in IntelliJ. However, some methods which should be available (e.g. the inherited getDDRM() method of the class SimpleMatrix) are not recognized and I can't use them. Whats very weird is that the very same procedure used with Scala (also using IntelliJ) works. That is, in Scala I can access the method - with Kotlin I can't.
It would be great if someone could shed some light on this.
update:
the getDDRM() method is part of the abstract class "Class SimpleBase<T extends SimpleBase>" and has the following signature: "public DMatrixRMaj getDDRM()". In my code I call this method on an instance of class SimpleMatrix which is a concrete class inheriting the SimpleBaseClass –
I should also note that I rebuild it with gradle and the issue still persists.
IMPORTANT: I should add that I can access other methods defined in the very same class. For instance I can access the method getMatrix() which is just another method of the very same (abstract) class. In fact, IntelliJ's method completion shows me a whole list of methods - but the getDDRM() is missing. I really don't get the cause of this problem.
Update 2:
If it is of any help: When I do not use gradle but instead open a Kotlinproject in IntelliJ and add the libary jars manually then everything works fine. Can anyone explain this?
Thanks in advance!

Where is Java Main method defined in source?

Pardon me for asking this silly question. Where can i find java main method definition in java source? it is not in object class or system class? so how and where is it defined exactly?
public static void main(String[] args) {}
Where is Java Main method defined in source?
It is declared in a class. Conventionally, it is a top-level (i.e. non-nested) public class, but that is not a requirement. (A non-public class will work, and I think a static nested class will work too.)
How do you find the main method?
Use grep or similar to search the source code of your application.
Use your IDE's method search capability.
Read the application's user documentation or launch script.
Look for the main method in the index of the application's javadoc.
How does the java command find it?
It doesn't! You specify the fully qualified class name of the class containing the main method you want to use on the java command line. Alternatively, you can set the Main-Class attribute in a JAR file's manifest so that the user doesn't need to know the class name.
UPDATE - If you are looking for the code in the OpenJDK source tree that loads the entrypoint class, finds the main method and invokes it, it is all in "jdk8u/jdk/src/share/bin/java.c". Happy reading.
It's not defined anywhere as code (in the standard libraries).
The JVM expects to find it if you're running a class, and if it's not found you get an error. Therefore it's up to you to create a public static void main(String[] args) method if you want to run your class.
main method is the entry point of an application in java. All the java classes are packaged as libraries which will be used in any application. So class files are used as references instead of separte executable. You can't execute the java source code separately because there won't be any main method definition in java source code.

AspectJ, separating native library calls from application calls

I am using AspectJ and Load-time weaving to trace methods calls in an arbitrary java program. I can trace all calls using the standard:
call(* *.*(..))
But what I now trying to do is separate out calls to the native java libraries and any application code:
nativeCalls(): !within(MethodTracer) && call(* java..*.*(..));
appCalls(): !within(MethodTracer) && call(* *.*(..)) && !call(* java..*.*(..));
The issue is that the nativeCalls() pointcut is picking out calls to application classes that inherit from native java classes, even though the signatures do not start with java.lang. or java.util, etc.
For example:
If I have a class tetris.GameComponent that inherits from java.awt.Component, my nativeCalls() pointcut will pick out tetris.GameComponent.getBackground() when the method is actually implemented in java.awt.Component.getBackground().
Is there a way to have my nativeCalls() pointcut ignore the calls to inherited methods?
I hope this is clear. I can provide additional info if necessary. Thanks for any help that can be provided.
Actually I have no idea why you want to exclude those inherited method calls from your trace because IMO it is important or at least interesting to know if a method was called on one of your classes, even if that method was defined in a JDK super class.
But anyway, the answer is no, you cannot exclude calls to JDK methods from your nativeCalls() pointcut if those calls are actually made upon target objects typed to one of your application classes. At the time the call is made, AspectJ does not know how the JVM will resolve the polymorphism. There can be several cases:
Call to Foo.aaa(), existing method Foo.aaa() is executed. This is the simple case where a called method actually exists.
Call to Foo.bbb(), inherited method Base.bbb() is executed (polymorphism). This is the case you want to exclude, but you cannot because the fact that a base method is called will only be known when the method is executed. Furthermore, if Base is a JDK class, you cannot even intercept its method executions with AspectJ.
Call to Base.ccc(), non-overridden method Base.ccc() is executed. This can happen if you directly create an instance of Base or also if you assign/cast a Foo instance to a variable typed Base, e.g. Base obj = new Foo(), and call obj.ccc() which has not been overridden by Foo.
Call to Base.ddd(), overridden method Foo.ddd() is executed (polmorphism). This also happens if you assign/cast a Foo instance to a variable typed Base, e.g. Base obj = new Foo(), and call obj.ddd() which has been overridden by Foo.
So much for not being able to easily exclude the polymorphism stuff when calling inherited JDK method.
Now the other way around: You can easily intercept execution() instead of call() upon your application classes and take advantage of the fact that JDK method executions cannot be intercepted anyway: pointcut appMethod() : execution(* *(..));

In Groovy, if class is not called why instantiation exception?

When I execute the following experimental code subset at http://groovyconsole.appspot.com/
class FileHandler {
def rootDir
FileHandler(String batchName) {
rootDir = '.\\Results\\'+batchName+'\\'
}
}
//def fileHandler = new FileHandler('Result-2012-12-15-10-48-55')
An exception results:
java.lang.NoSuchMethodException: FileHandler.<init>()
When I uncomment the last line that instantiates the class, the error goes away.
Can someone explain why this is? I'm basically attempting to segregate the definition and instantiation of the class into 2 files to be evaluated separately. Thanks
I'm not sure of the exact implementation details behind http://groovyconsole.appspot.com/ (source linked to from there points to Gaelyk, which I've not looked over). I'd bet it's looking for a no-arg constructor for the class you've presented, in an effort to find something runnable. (note that if you provide that, it still won't work, as it wants a main() :/)
Running locally in groovyConsole will die a bit sooner, with the following error message:
groovy.lang.GroovyRuntimeException: This script or class could not be run. It should either:
- have a main method,
- be a JUnit test or extend GroovyTestCase,
- implement the Runnable interface,
- or be compatible with a registered script runner.
This is perhaps more descriptive and to the point. If you want to run some Groovy as a simple script, you'll need to supply a jumping-in point. The easiest way is an executable statement in your groovy file, outside of any class definition (e.g, uncommenting your instantiation statement). Alternatively, a class with a main method should do it. (see here).
If 2 files is how you want to break it up, you can save the class file def in one groovy file (e.g., First.groovy) and create a second (e.g., Second.groovy) with just your executable statements. (I believe the first one will be in the classpath automatically when you run groovy Second, if both are in same directory)

Does Matlab have something akin to a main method?

If I have a single Matlab source file (m-file) containing a class definition (classdef), is there any way to specify a particular set of code that is to be executed if I run the m-file? I mean the entire file, such as via the Run button in the IDE, from a shell, or from the Matlab command-line. I don't mean manually selecting the code to be executed.
Similar behaviour exists in Java with the static main method and in Python by having code outside the class definition (possibly inside a if __name__==__main__ block).
The short answer is "no"; MATLAB classdef M-files are just intended to define objects, not form complete programs.
The long answer is you might be able to get specific behavior out of your classdef function if, for example, you overload the constructor to take a flag specifying whether or not to "act like a variable" or "act like a program".
e.g.
classdef myClass
...
methods
function self = myClass(varargin)
if nargin == 1 && strcmpi(varargin{1},'run')
..... %run the program
else
..... %make the variable
OR you can make a static method called main:
methods (Static = true)
function main()
%enabes: myClass.main()
...
end
The IDE still won't know what to do with your M-file to "run it", but you can run it properly from the command line, or another M-file.
That last sentence is not 100% correct - as Egon pointed out below, you CAN make MATLAB's IDE run that code - use a "run configuration": http://www.mathworks.com/help/matlab/matlab_prog/run-functions-in-the-editor.html
There are a few ways you can do this:
You can create a "run configuration" (either as a script or a specific line of code). This will run whenever you click the run button (or press the run shortcut) from within your classdef file. The big drawback is that those run configurations are stored locally, so this is a nightmare when it comes to collaboration or working at multiple places. So personally, I'd recommend writing a script if you have a complicated run configuration. Mine are mostly called testMyClass where MyClass of course is the class you want to run.
If you don't require complicated code, you can also put everything in the Constructor of your object. If you check whether there are no arguments passed with if nargin == 0 ... end, that piece of code should be called whenever you 'run' the class file. However, you are somewhat limited in what you can do, as you could create infinite loops or an infinite chain of those objects being created if you are not careful. In the end, you will have just the object in your base workspace.
If you do require more complicated code or some code that makes some variables within the base workspace, it can be accomplished but it comes at great cost. Your code might end up a complete mess, so I advise against using this unless you have an extremely good reason otherwise. You can use the previous method and the evil functions evalin and assignin to evaluate and assign to variables in the base workspace.