13

There is a java file, which has some dependencies jars. But now, I don't have those jars, and I have to compile it to a .class file.

Is it possible to do this?


UPDATE

Thanks for your answers.

At first, I thought we can create some stubs for the missing dependencies, that's easy but boring. Since we can create the stubs without missing stubs to make the compiler happy, why can't we make a tool do it automatically? The tool doesn't need to create stubs, but reads the java file, collects informations, and then builds the .class files.

But if the "import" statements in the java file contain "*", that will be a problem:

import aaa.*
import bbb.*

public class Hello {
   World world;
}

We don't know if the class "World" is under package "aaa" or "bbb". If we are not familiar with the missing dependencies, we even don't know how to create a stub for the class "World".

But if the "import" statements are clear, I think it's possible, but maybe no one will write such a tool

David Brossard
  • 13,584
  • 6
  • 55
  • 88
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • My guestion is: Having done so, what will you do with the class file? How can you be sure the code is correct if you can't run it to test it? – T.J. Crowder Jun 22 '11 at 06:51
  • If you do Android development, the Android tools come with stub versions of all the classes you need for the Android SDK. Run `javap` on the class files in `android.jar` to see what I mean. Is it possible that someone's job is to sit there and write stubs all day long? Yes. Is it likely? No. So you could try browsing the Android code base and see what they do. If I was going to write such a tool myself, I would probably start with Codemodel (via http://stackoverflow.com/questions/121324/a-java-api-to-generate-java-source-files) – Tyler Nov 02 '11 at 01:35
  • possible duplicate of [Disabling compile-time dependency checking when compiling Java classes](http://stackoverflow.com/questions/1537714/disabling-compile-time-dependency-checking-when-compiling-java-classes) – Ciro Santilli OurBigBook.com Mar 18 '15 at 11:00

4 Answers4

13

You could go crazy and hand craft the required dependencies as stubs that do nothing except keep the compiler happy.

Tom
  • 43,583
  • 4
  • 41
  • 61
  • 1
    +1 - No need to go crazy though, just hover over the classes you're missing in Eclipse and click "Create class 'xxxx'" in the context menu. You can do the same with all the required methods in missing classes. – Qwerky Jun 22 '11 at 09:15
  • And when i want to call the real method from the real jar? – Gobliins Oct 20 '15 at 11:57
  • You'd need the real dependencies there at runtime the stubs would only there be their for compilation, using a mocking framework could help for testing. – Tom Oct 21 '15 at 18:46
5

No. Sorry. You'll need all dependncies in the classpath to compile.

Asaph
  • 159,146
  • 25
  • 197
  • 199
3

No. But you could provide stubbed-out versions of the dependency class files, if it is only a handful of classes that the code your are trying to compile uses directly.

Then in theory if you take the .class file that compiles and place the real dependencies on the classpath with it your app will work using the correct (non-stubbed-out) dependency classes.

aroth
  • 54,026
  • 20
  • 135
  • 176
1

Before any file is compiled it always looks up for any dependencies. but you said you dont have those jars!!!

see if you can remove the dependencies relation for that project/file and then try to compile it. give it a try!

Rohit A.
  • 1,775
  • 2
  • 11
  • 9