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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to jar the resulting .class files after compilation

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello all,
Right now the code that I have posted below will compile the two .java files provided....and it will also jar these files...But I want to jar the resulting .class files that get generated after the compilation....

However, the compile method doesn't return any files, it returns a boolean that indicates whether the compile was successful or unsuccessful. After a successful compilation The .class files are placed inside my working directory. As the code works now, it jars the .java files because those are the files that i past it. Can anyone help me to figure out how to jar the .class files that result from the compile?


//This method allows the user to name the jar file and choose the files to jar

private File interfaceFile;
private File handlerFile;

public void compileAndJar()
{
System.out.println("Entering the compileANdJar() Method....");

// These are the files to include in the Jar file
String[] filenames = new String[]{interfaceFile.getName(), handlerFile.getName()};
System.out.println("INTERFACE FILE NAME----> "+interfaceFile.getName()+" HANDLER FILE NAME----> "+handlerFile.getName());

//compile the interface and handler files
try
{
sun.tools.javac.Main comp = new sun.tools.javac.Main(System.out, null);
boolean iFacesuccess = comp.compile(filenames);
System.out.println(comp.compilationPerformedSuccessfully());
}
catch(Exception e)
{
e.printStackTrace();
}

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try
{
// Create the jar file
String outFilename = jarTextField1.getText(); //Allow the user to set this variable
System.out.println("JAR FILE NAME----> "+outFilename);

JarOutputStream out = new JarOutputStream(new FileOutputStream(outFilename));

// Compress the files
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);

// Add JAR entry to output stream.
out.putNextEntry(new JarEntry(filenames[i]));

// Transfer bytes from the file to the JAR file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the JAR file
out.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
[ June 15, 2005: Message edited by: Durand Grimmett ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Continued here.
 
    Bookmark Topic Watch Topic
  • New Topic