0

I am having an argument with my professor which I cant seem to find an answer to in google... My question is, when I use a method that is in another class, the JVM will try and find the class's .class file right? and the argument is this: if the .class file is not found will there be a search for the source file and if found it will be recompiled or will it throw an exception? Thanks!

UPDATE: I rephrased the question, I will be very grateful for your help!

7
  • 1
    JVM is not responsible for compiling the source code. Commented Feb 11, 2014 at 9:28
  • JVM doesnt compile source code, so it will through an exception directly Commented Feb 11, 2014 at 9:29
  • Then whoever is responsible for it, because I know that this process does happen when you compile your project Commented Feb 11, 2014 at 9:30
  • 1
    @SaddamAbuGhaida Actually it can compile "things". E.g. it can contain a JIT compiler to compile bytecode to native code. But it doesn't compile source code, here you are correct. Commented Feb 11, 2014 at 9:30
  • @user2466613 Your IDE might have a compile on save feature. Commented Feb 11, 2014 at 9:31

3 Answers 3

2

It is not at all the standard behavior, and will not work in a normal environment.

However, I have heard of some commercial app server distributions that were able to do that... someone told me about older weblogic versions, configured in development mode, but maybe just an urban legend :P

I guess that you could write a tuned classloader that looked for .java files in the classpath and, given it runs on a JDK with a java compiler, do what you say.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, such a classloader would not even be too complicated. The only question is: who reads the error messages. :)
1

The standard JRE does not even include a compiler for Java source code. To compile Java source code you need a JDK or an IDE which includes its own Java compiler. If you compile your source code using javac or the IDE’s built-in compiler, these compilers will search for source files of the referenced classes and compile them if the .class file was not found or if the source file is newer than the class file. But if you, e.g. delete the class file of the referenced class afterwards and run your program, the JVM won’t search for a source file. It will throw an NoClassDefFoundError.

Comments

0

JVM will not search for source file for the class whose method we are trying to invoke. JVM will throw a exception like:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Employee cannot be resolved to a type
    Employee cannot be resolved to a type
    at com.test.TestIt.main(TestIt.java:7)

Employee is the class whose method we want to call(.class file is not created for Employee)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.