0

I was curious about the differences between .jar with .class files and .jar with .java files. I partially got the answer here, But then what is the usefulness of .java files in the jar?

My guess is that the java files in the jar are like an interface that prevents compilation error, because I solved the IllegalAccessError thrown on runtime by replacing jar files with .class with jar files with .java specifically when using Xposed Framework. (Got the hint from this thread.)

Also

Thank you for your explanations and they were helpful. But I want to learn more about the differences in compiler's view, because I am wondering why my app works fine even if I only included the jar with java files, not class files (zxing). Also there are some cases that throws IllegalAccessException when I include the jar with class files, but not thrown when I include the jar with java files(xposed), even though I have to include at least one of them to make the compiler(AIDE) not complain about references, like unknown package. Why does the compiler not complain when I include only jar with java files though the compiler would not be able to resolve the actual implementation of the referred classes?

KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30

2 Answers2

1

Its simple - *.java files are sources, *.class files are compiled classes.

What is used on runtime by JVM?? *.class files. Why would you put source files inside library? IDK, usally sources are distributed as separate jar, but all in all it is done to allow you to check library code without decompilation.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
1

A .jar file is basically just a .zip file with another extension.

A .jar file with .class files have a special purpose and may have special meta-data (e.g. in META-INF folder).

A .jar file .java files is just a .zip file.

It is however common for open-source libraries to provide 3 .jar files:

  • One with .class files, to be used by your code, both to compile and to run your code.

  • One with .java files, to be used by your IDE, so you can drill into the library code and see it. Especially useful when stepping through the code with a debugger.

  • One with javadoc files (.html files), to be used by your IDE, so you can read the documentation about the classes and methods in the library. You do read the documentation, right?

None of those 3 files have to be named .jar. They could be renamed .zip so you could easily open them in your favorite Zip utility, or they could be renamed .foo just because...

They should be named .jar, to clarify that they are Java ARchives.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you, you helped me to reduce number of qusetions I would have to ask, the javadoc.jar. – KYHSGeekCode Mar 19 '18 at 22:22
  • I understood your explanation and it was helpful. But I want to learn more about the differences in compiler's view, because I am wondering why my app works fine even if I only included the jar with java files, not class files. Also there are some cases that throws IllegalAccessException when I include the jar with class files, but not thrown when I include the jar with java files, even though I have to include at least one of them to make the compiler(AIDE) not complain about references, like unknown package. Why does the compiler not complain when I include only jar with java files? – KYHSGeekCode Mar 20 '18 at 10:23
  • Though the compiler would not be able to resolve the actual implementation of the referred classes. – KYHSGeekCode Mar 20 '18 at 10:25
  • Or do I have to ask new question? – KYHSGeekCode Mar 23 '18 at 13:27