1

So, I have two jar files. Foo.jar, and Bar.jar.

Bar.jar has a Main.class holding

public class Main {
    public static String bob(){
       return "bob";
    }
}

And then in Foo.jar, i want to be able to access the "bob" method, without including the jar as a library or resource or anything like that.. It's just going to be a completely separate jar.. Is that even possible..?

Sorta like this

System.out.println(BarAsJarFile.Main.bob());
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • 1
    I don't understand the question. What you mean is that you don't want to run java -cp Foo.jar:Bar.jar ? If so, why not? – Diego Basch Dec 26 '12 at 02:58
  • 1
    It's possible. But why do you wish to do it? What application feature does this provide? – Andrew Thompson Dec 26 '12 at 02:58
  • They can update Bar.jar, without updating Foo.jar. That way, i don't have to use it as a library, but can still reach the method. And what does "java -cp Foo.jar:Bar.jar" do..? Does that add Bar.jar as a library to Foo.jar..? or something? (I'm really new to Java) – Nathan F. Dec 26 '12 at 03:01
  • Do you mean they can update Bar.jar *while the program is running*? If so, that is very tricky. If not, `java -cp Foo.jar:Bar.jar` should work fine. – Thilo Dec 26 '12 at 03:02
  • Can someone tell me exactly what "java -cp Foo.jar:Bar.jar" does?.. and can i do java -cp Foo.jar:Bar.jar fisc.cl.hereitis.Main to access a class in Foo.jar? – Nathan F. Dec 26 '12 at 03:03
  • @thefiscster510: How do you start your application now? You must be setting a classpath somewhere already. – Thilo Dec 26 '12 at 03:07
  • 1
    The jar file probably contains a manifest which indicate which class contains the main method, which would be the entry point for the JVM. http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html – ltfishie Dec 26 '12 at 03:11
  • *"They can update Bar.jar, without updating Foo.jar."* Umm.. OK. Do these apps. have a GUI? If so, [JWS](http://stackoverflow.com/tags/java-web-start/info) might be the answer. Caveat though, I still don't really understand the question. – Andrew Thompson Dec 26 '12 at 03:14
  • No, I have multiple main methods in a single jar.. for starting with different environment variables / working directories. – Nathan F. Dec 26 '12 at 03:14
  • Sorry that the question isn't really descriptive.. It's kinda hard to explain what I'm doing.. – Nathan F. Dec 26 '12 at 03:15

1 Answers1

0

I don't understand why you cannot set the classpath when you start the JVM (you can easily include multiple jar files that way), but it is possible to add jar files to your classpath later programmatically.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656