• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing Java file inside a jar file

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

1) Is it possible to execute a java file from with in a jar file.
Say "A.jar", having "B.java" with it. Is it possible to execute (both compile and run using javac and java) the "B.java" file which resides inside the jar file.

2) If we can execute B.java, Can it be possible to refer to external resource. say "C.txt" from the java file.

The idea behind is liked to have both the client (standalone java application) and its dependency class file and other text file together as a single file.

Really sorry if this question means absurd to you.
[ October 10, 2006: Message edited by: Parameswaran Thangavel ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "javac" compiler works only on disk files, so no, you can't use that. But there are other compilers, and it's possible that the one that's built into the Eclipse Java Development Tools can do this.

Could code in a jar refer to files outside the jar? Sure, using java.io.File. Can it refer to files inside the jar? Sure, using ClassLoader.getResourceAsStream().

Now, I can't figure out why you'd want to ship some of the source in a jar file and have it compiled after its deployed; why not just compile it ahead of time?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Parameswaran Thangavel:
Hi all

1) Is it possible to execute a java file from with in a jar file.
Say "A.jar", having "B.java" with it. Is it possible to execute (both compile and run using javac and java) the "B.java" file which resides inside the jar file.

2) If we can execute B.java, Can it be possible to refer to external resource. say "C.txt" from the java file.

The idea behind is liked to have both the client (standalone java application) and its dependency class file and other text file together as a single file.

Really sorry if this question means absurd to you.

[ October 10, 2006: Message edited by: Parameswaran Thangavel ]


Well, you can use BCEL (byte code engineering) but then you'd have to include their jar file too. You might also want to look into Sun's com.sun.tools.javac.Main, as you might be able to write similar code (make sure you don't violate and license laws) in your own class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic