Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Executing Java file inside a jar file

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Are you here to take over the surface world? Because this tiny ad will stop you!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic