This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Executing Java file inside a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Executing Java file inside a jar file" Watch "Executing Java file inside a jar file" New topic
Author

Executing Java file inside a jar file

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Executing Java file inside a jar file
 
Similar Threads
Problem in compiling classes
using .exe in the .jar
main method invocation possible from another class?
Executing a *.bat file
How to Execute a .exe file stored in jar file ?