Where is Java Main method defined in source? - program-entry-point

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.

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!

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

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.

Javassist NotFoundException when getting java.io.Serializable with JDK9

I have the following code:
private static CtClass resolveCtClass(String clazz) throws NotFoundException {
ClassPool pool = ClassPool.getDefault();
return pool.get( clazz );
}
When running under JDK8, if this method is called using java.io.Serializable, it works, but when running under the JDK9 environment, it throws the NotFoundException.
Is there something I overlooked here?
This does no longer happen with the current EA builds of Java 9. Class files are now always locatable even if they are encapsulated in a module.
This is a consequence of Java 9's module encapsulation where non-exported resources are no longer available via the ClassLoader API. Under the covers, Javassist calls
ClassLoader.getSystemClassLoader().findResource("java/io/Serializable.class");
to get hold of the class file for Serializable. It then parses this class file and represents the information similarly to the Java reflection API but without loading the class such that it can be edited prior to loading it.
Until Java 8, this class file was accessible as most class loaders rely on looking up a class file before loading it such that the above call returned a URL pointing to the file. Since Java 9, resources of named modules are only available via the new API method findResource(String, String) where the second arguments names the module of that class.
The short answer is: Javassist does no longer work with Java 9 and none of its dependant projects will. This is a known issue with the current Java 9 implementation and will hopefully be fixed prior to release.
(I never used Javassist so I'm just shooting in the dark, here...)
The documentation of ClassPool says:
If get() is called on this object, it searches various sources represented by ClassPath to find a class file and then it creates a CtClass object representing that class file.
This seems to be bound to the concept of the class path. Looking at ClassPath and CtClass supports that assumption.
If that is the case, then Javassist might just not be fit to look into JDK 9's brand new modules.
If my guess is correct, you should not be able to get any JDK class from the pool. This should be easily verifiable.

Is CamelGroovyMethods used as a groovy category?

Apache Camel comes with some relatively nice Groovy extensions so that you, for instance, can use closures with the Java DSL for defining routes.
Most, if not all, of the additional methods providing these extensions seem to be located in the class CamelGroovyMethods with static methods like
public static ProcessorDefinition<?> process(ProcessorDefinition<?> self,
Closure<?> processorLogic){/* implementation */}
How is the actual extension of the Camel java classes realised? Is CamelGroovyMethods used as a category somewhere, and if so, where is use(CamelGroovyMethods) called?
Just a guess, but as they are called extension methods they have probably been defined as such. Look in the jar, you should find a file called org.codehaus.groovy.runtime.ExtensionModule in META-INF/services. Have a look at Creating an extension module. I've used this technique myself and it works great except if you want to provide custom constructors, that requires an alternate mechanism.
...
Yep, found it ExtensionModule file in GitHub. They even provided the dsld file to assist with code completion in Eclipse.

Is it possible to get the Extension class loader object?

I'm learning about ClassLoading concepts in java.
I know that can get the System classLoader with ClassLoader.getSystemClassLoader() method using java.lang.ClassLoader class. Is it possible to get Extension class loader also?
I'm aware that JVM loads the BootStrapClassLoader and we cannot instantiate it. But what about Extension class loader?
Thanks in Advance.
You can try this one sun.net.spi.nameservice.dns.DNSNameService.getClassLoader(). DNSNameService is one the class that exist as the Extension class loader.
Another option is to write something like this:
ClassLoader cl =new Object(){}.getClass().getEnclosingClass().getClassLoader();
ClassLoader prev = null;
while(cl!=null){
prev=cl;
cl=cl.getParent();
}
System.out.println(prev);
prev will contain reference to extension class loader.
Note: You can write besides
new Object(){}.getClass().getEnclosingClass().getClassLoader() idiom Thread.currentThread().getContextClassLoader() or even simpler YourClassName.class.getClassLoader() While these idioms are not identical any of them will do the job.
See http://www.javacodegeeks.com/2011/03/understanding-extending-java.html for some more details.