Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-24-2004, 03:09 PM   #1 (permalink)
freesoft_2000
Code Monkey
 
Join Date: Oct 2004
Posts: 51
freesoft_2000 is on a distinguished road
Question JarOutputStream

Hi everyone,
I am currently trying to create a tool that jars all my classes into an executable jar. What i mean is that when i click on the particular jar file in windows explorer the program is run. I managed to create the jar file but the problem is that when i click on the jar file the program is not excuted and the java virtual machine says that the manifest file could not be read.

When i try to unpack the jar file all my classes are there but my manifest file always remains empty.This is a pretty long program so apologize for any inconvinience caused. And yes the manifest file is placed in a separate folder.

The program is as follows:

Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.jar.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class JPackager implements ActionListener

{

JFrame fr = new JFrame ("Frame");
JDialog Dialog1 = new JDialog(fr, "Select The Main Method");

JLabel Label1 = new JLabel("Label1                        ", SwingConstants.RIGHT);
JLabel Label2 = new JLabel("Add Java Class Files To The Jar Archive");
JLabel Label3 = new JLabel("Select The Class With The Main Method");

JButton Button1 = new JButton("Add Java Classes To List");
JButton Button2 = new JButton("Create Jar");
JButton Button3 = new JButton("Exit");
JButton Button4 = new JButton("Create Jar File");
JButton Button5 = new JButton("Close");
JButton Button6 = new JButton("Remove Selected File");
JButton Button7 = new JButton("Clear List");

JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();

//The below command line creates the group for the JRadioButton

ButtonGroup Group1 = new ButtonGroup();

JRadioButton RadioButton1 = new JRadioButton("Auto Create Manifest", true);
JRadioButton RadioButton2 = new JRadioButton("Manual Create Manifest", false);

DefaultListModel list1 = new DefaultListModel();
DefaultListModel list2 = new DefaultListModel();

JTextArea TextArea1 = new JTextArea("Text Area",5, 30);

//The below list displays the class file names to be added to the jar archive
 
JList List1 = new JList(list1);
//The below list displays the class file names to be added to the jar archive

JList List2 = new JList(list2);

JScrollPane ScrollPane1 = new JScrollPane(List1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
                                          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                                          
JScrollPane ScrollPane2 = new JScrollPane(List2, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
                                          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                                          
JScrollPane ScrollPane3 = new JScrollPane(TextArea1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
                                          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

                    
JFileChooser FileChooser1 = new JFileChooser();
JFileChooser FileChooser2 = new JFileChooser();  

Manifest manifest;                                                              

Dimension Size1 = new Dimension();
Dimension Size2 = new Dimension();

public void initialize ()
{ 
Container pane1 = fr.getContentPane();
pane1.setLayout(new FlowLayout());
fr.setSize(250,300);
fr.setLocation(300,300);
fr.setBackground(Color.lightGray);
Size1.width = 350;
Size1.height = 250;
List1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
List1.setLayoutOrientation(JList.VERTICAL);
ScrollPane1.setPreferredSize(Size1);
ScrollPane1.setColumnHeaderView(Label2);
pane1.add(ScrollPane1);

pane1.add(Button1);
pane1.add(Button2);
pane1.add(Button3);
pane1.add(Button6);
pane1.add(Button7);

Size2.width = 350;
Size2.height = 250;
TextArea1.setLineWrap(false);
TextArea1.setWrapStyleWord(false);
ScrollPane1.setPreferredSize(Size2);
pane1.add(ScrollPane3);

pane1.add(Label1);

// Use the JAVA constant JFrame.HIDE_ON_CLOSE for subsequent forms in multi form
// applications

fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button6.addActionListener(this);
Button7.addActionListener(this);
fr.pack();
fr.setVisible(true);
}

public void initializedialog1 ()
{ 
Container pane2 = Dialog1.getContentPane();
pane2.setLayout(new BorderLayout());
Dialog1.setSize(250,300);
Dialog1.setLocation(300,300);
Dialog1.setBackground(Color.lightGray);

List2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
List2.setLayoutOrientation(JList.VERTICAL);
ScrollPane1.setColumnHeaderView(Label3);

pane2.add(ScrollPane2, "Center");

initializepanel1();
pane2.add(Panel1, "North");

initializepanel2();
pane2.add(Panel2, "West");

// Use the JAVA constant JFrame.HIDE_ON_CLOSE for subsequent forms in multi form
// applications

Dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Dialog1.pack();
Dialog1.setVisible(true);
}

public void initializepanel1 ()
{ 
Panel1.setLayout(new FlowLayout());
Panel1.add(Button4);
Panel1.add(Button5);
Button4.addActionListener(this);
Button5.addActionListener(this);
}

public void initializepanel2 ()
{ 
Panel2.setLayout(new BoxLayout(Panel2, BoxLayout.Y_AXIS));

//The below three command lines adds the JRadioButton to a group

Group1.add(RadioButton1);
Group1.add(RadioButton2);

Panel2.add(RadioButton1);
Panel2.add(RadioButton2);
}

public void autocreatemanifest (String str1)
{

String str2 = null, str3 = null;
String str4 = null;

int w = 0, w1 = 0;

str2 = str1;
//The below command line removes the extension of the java class

w = str2.lastIndexOf(".");
str3 = str2.substring(0, w);
//The below command line removes the path of the java file name

w1 = str3.lastIndexOf("\\");
str4 = str3.substring(w1 + 1, str3.length());

try
{

StringBuffer StringBuffer1 = new StringBuffer();
StringBuffer1.append("Main-Class: " + str4);
StringBuffer1.append("\n");
StringBuffer1.append("\n");

ByteArrayInputStream is = new ByteArrayInputStream(StringBuffer1.toString().getBytes("UTF-8"));

manifest = new Manifest(is);
}

catch(Exception e)
{
Label1.setText("The manifest file could not be created");
}

}

public void manualcreatemanifest (String str5)
{

try
{
StringBuffer StringBuffer2 = new StringBuffer();
StringBuffer2.append(str5);
StringBuffer2.append("\n");
StringBuffer2.append("\n");

InputStream is1 = new ByteArrayInputStream(StringBuffer2.toString().getBytes("UTF-8"));

manifest = new Manifest(is1);
}

catch(Exception e)
{
Label1.setText("The manifest file could not be created");
}

}

public void insertfiles ()
{
int i = 0;

//The below three command lines sets the JFileChooser options to select 
//the file that is required by the user to be archived

FileChooser1.setMultiSelectionEnabled(true);
FileChooser1.setDialogType(JFileChooser.SAVE_DIALOG);
FileChooser1.setDialogTitle("Select the java classes files to archive");

if(FileChooser1.showDialog(fr,"Select files") != JFileChooser.APPROVE_OPTION)
{
return;
}

//The below command line passes the selected files to the 
//File[] array to be passed to the createarchive as an argument

File[] selected = FileChooser1.getSelectedFiles(); 

for(i=0;i<selected.length;i++)
{
list1.addElement(selected[i].toString());
list2.addElement(selected[i].toString());
}

}

public void listall ()
{
//This function archives all the files in the list box

Object[] p = list1.toArray();
String[] k = new String[p.length];
int j = 0;

//The below command line transfers the files in the list box 
//to a string array

for(j=0;j<p.length;j++)
{
k[j] = p[j].toString();
}

//The below two command lines sets the JFileChooser options to select 
//an output directory for the newly created archived file
 
FileChooser2.setDialogType(JFileChooser.OPEN_DIALOG);
FileChooser2.setDialogTitle("Select an output for the jar archive");

if(FileChooser2.showDialog(fr,"Archive") != JFileChooser.APPROVE_OPTION)
{
return;
}

//The below command line sets the file extension of the 
//user selected file
 
File f = FileChooser2.getSelectedFile();
String archive = (f.toString() + ".jar");

createjararchive(archive, k);
}

public void createjararchive (String str6, String[] str7)
{
byte buffer3[] = new byte[10240];
int k = 0, k1 = 0;
String s = null, g = null;

try
{
FileOutputStream stream = new FileOutputStream(str6);

JarOutputStream out = new JarOutputStream(stream, manifest);

for (k=0; k<str7.length; k++)
{

// Add archive entry
s = str7[k];
k1 = s.lastIndexOf("\\");
g = s.substring(k1+1);

JarEntry jarAdd = new JarEntry(g);
out.putNextEntry(jarAdd);

// Write file to archive
FileInputStream in = new FileInputStream(str7[k]);
				
while(true)
{

int nRead = in.read(buffer3, 0, buffer3.length);
					
if (nRead <= 0)
{
break;
}

out.write(buffer3, 0, nRead);
}

in.close();
}
			
out.close();
stream.close();
Label1.setText("The jar archive was created successfully");
}

catch (Exception e)
{
e.printStackTrace();
}

}

public void actionPerformed(ActionEvent event)
{
JComponent b = (JComponent)event.getSource();
int c = 0;
boolean d;
boolean e;
String str8 = null;

if(b == Button1)
{
insertfiles();
}

else if(b == Button2)
{
initializedialog1();
}

else if(b == Button3)
{
System.exit(0);
}

else if(b == Button4)
{

d = RadioButton1.isSelected();
e = RadioButton2.isSelected();

if(d == true)
{
str8 = (String)List2.getSelectedValue();
System.out.println(str8);
autocreatemanifest(str8);
listall();
}

else if(e == true)
{
str8 = TextArea1.getText();
manualcreatemanifest(str8);
listall();
}

}

else if(b == Button5)
{
Dialog1.setVisible(false);
}

else if(b == Button6)
{
c = List1.getSelectedIndex();
list1.removeElementAt(c);
list2.removeElementAt(c);
}

else if(b == Button7)
{
list1.clear();
list2.clear();
}

}   
public static void main(String args[])
{
JPackager a = new JPackager(); 
a.initialize();
}
}
I do not know why the manifest file is always empty although its not supposed to be.

I also apologize for the layout of the frame as it was done rather quickly.

i hope somone can take look at my code and tell me where i have gone wrong.

Once again i apologize for the long program

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
freesoft_2000 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting