# Java Programm um ein Jar Archiev zu erzeugen



## Zhoragh (18. Dezember 2005)

Ich möchte gern aus einem JavaProgramm mehrer Dateien in einem Jar Archiev packen inclusive Manifest 

Kann mir jemand erklären wie ich das realisieren kann und wie ich das Manifest manibulieren kann?


----------



## Thomas Darimont (18. Dezember 2005)

Hallo!

 Schau mal hier:

```
/**
  * 
  */
 package de.tutorials;
 
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.util.jar.Attributes;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
 
 /**
  * @author Tom
  * 
  */
 public class JarFileCreationExample {
 
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) throws Exception {
 		Manifest manifest = new Manifest();
 		Attributes mainAttributes = manifest.getMainAttributes();
 		mainAttributes.put(new Attributes.Name("main-class"), HelloWorld.class
 				.getName());
 
 		JarOutputStream jarOutputStream = new JarOutputStream(
 				new FileOutputStream("c:/tutorials.jar"), manifest);
 		ZipEntry zipEntry = new ZipEntry(HelloWorld.class.getName().replace(
 				".", "/")
 				+ ".class");
 		jarOutputStream.putNextEntry(zipEntry);
 		InputStream inputStream = JarFileCreationExample.class
 				.getResourceAsStream("HelloWorld.class");
 		byte[] buffer = new byte[1024];
 		int bytesRead = 0;
 		while ((bytesRead = inputStream.read(buffer)) > 0) {
 			jarOutputStream.write(buffer, 0, bytesRead);
 		}
 		inputStream.close();
 		jarOutputStream.closeEntry();
 		jarOutputStream.flush();
 		jarOutputStream.close();
 	}
 }
```
 

```
/**
  * 
  */
 package de.tutorials;
 
 /**
  * @author Tom
  *
  */
 public class HelloWorld {
 
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) {
 		System.out.println("Hello World!");
 	}
 
 }
```
 
 Anschließend in der Konsole:

```
C:\>java -cp .\tutorials.jar de.tutorials.HelloWorld
 Hello World!
```
 
 Gruss Tom


----------



## Zhoragh (18. Dezember 2005)

Ich hab jetz ein Archiev erstellt und die Passende jad gleich mit.

mein Manifest(My.jar)

Manifest-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MIDlet-Name: MyClass
Created-By: 1.5.0_04 (Sun Microsystems Inc.)
MIDlet-Vendor: RedMoonGames
MIDlet-1: MyClass, , MyClass
MIDlet-Version: 1.0
MicroEdition-Profile: MIDP-2.0

meine Jad(My.jad)

MIDlet-1: My, , My
MIDlet-Jar-Size: 451367
MIDlet-Jar-URL: My.jar
MIDlet-Name: My
MIDlet-Vendor: Ich
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0 

Die grösse der Datei stimmt

Wenn ich jetzt das Midlet per emulator ausführen möchte startet er zwar zunächst aber sobald ich das Midlet starte gibt er eine lange  Fehlermeldung aus.

Running with storage root temp.MediaControlSkin1134878581750
Method............: f26cb4 'MyGameCanvas.<clinit> (static)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb305c
Current IP........: f2559d = f25524 + offset 121
Previous Frame....: eb3038
Previous IP.......: 101d4033 (offset 0)
Frame size........: 0 (0 arguments, 0 local variables)
Operand[1]........: 4

Method............: 1015a374 'java/lang/Class.runCustomCode (static)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb3038
Current IP........: 101d4033 = 101d4033 + offset 0
Previous Frame....: eb3020
Previous IP.......: f2957c (offset 0)
Frame size........: 0 (0 arguments, 0 local variables)
Operand[1]........: 10001925
Operand[2]........: f29864
Operand[3]........: 5

Method............: f295d4 'My.startApp (virtual)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb3020
Current IP........: f2957c = f2957c + offset 0
Previous Frame....: eb3004
Previous IP.......: 101f3702 (offset 7)
Frame size........: 1 (1 arguments, 0 local variables)
Argument[0].......: f24ee4

Method............: 1016f93c 'javax/microedition/midlet/MIDletProxy.startApp (virtual)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb3004
Current IP........: 101f3702 = 101f36fb + offset 7
Previous Frame....: eb2fe8
Previous IP.......: 101d43ae (offset 270)
Frame size........: 1 (1 arguments, 0 local variables)
Argument[0].......: f24ebc

Method............: 1015a558 'com/sun/midp/midlet/Scheduler.schedule (virtual)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb2fe8
Current IP........: 101d43ae = 101d42a0 + offset 270
Previous Frame....: eb2fb4
Previous IP.......: 101dec85 (offset 28)
Frame size........: 7 (2 arguments, 5 local variables)
Argument[0].......: eb298c
Argument[1].......: eb2138
Local[2]..........: f24ebc
Local[3]..........: 2
Local[4]..........: eb2980
Local[5]..........: f29638
Local[6]..........: 1015a478

Method............: 1016224c 'com/sun/midp/main/Main.runLocalClass (static)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb2fb4
Current IP........: 101dec85 = 101dec69 + offset 28
Previous Frame....: eb2f90
Previous IP.......: 101de6fc (offset 116)
Frame size........: 3 (1 arguments, 2 local variables)
Argument[0].......: eb2e34
Local[1]..........: eb2138
Local[2]..........: f29600

Method............: 101621ac 'com/sun/midp/main/Main.main (static)' 
Stack Chunk.......: eb2f7c
Frame Pointer.....: eb2f90
Current IP........: 101de6fc = 101de688 + offset 116
Previous Frame....: 0
Previous IP.......: 1
Frame size........: 3 (1 arguments, 2 local variables)
Argument[0].......: eb3268
Local[1]..........: eb2e34
Local[2]..........: eb279c

VM status:
Instruction pointer.: f2559d (offset within invoking method: 121)
Next instruction....: 0xbd
Frame pointer.......: eb305c
Local pointer.......: eb305c
Stack size..........: 128; sp: eb3074; ranges: eb2f84-eb3184;
Contents of the current stack frame:
    eb305c: eb3038 (lp) (fp)
    eb3060: 101d4033
    eb3064: eb3058
    eb3068: f26cb4
    eb306c: eb2f7c
    eb3070: 0 (end of frame)
    eb3074: 4 (sp)
Execution stack contains 244 items: 
eb3268  
eb2e34  
eb279c  
0  
1  
f295f0  
101621ac  
eb2f7c  
0  
eb2e34  
eb2138  
f29600  
eb2f90  
101de6fc  
eb2fa4  
1016224c  
eb2f7c  
0  
eb298c  
eb2138  
f24ebc  
2  
eb2980  
f29638  
1015a478  
eb2fb4  
101dec85  
eb2fc8  
1015a558  
eb2f7c  
0  
f24ebc  
eb2fe8  
101d43ae  
eb2ffc  
1016f93c  
eb2f7c  
0  
f24ee4  
eb3004  
101f3702  
eb3018  
f295d4  
eb2f7c  
0  
eb3020  
f2957c  
eb3034  
1015a374  
eb2f7c  
0  
10001925  
f29864  
5  
eb3038  
101d4033  
eb3058  
f26cb4  
eb2f7c  
0  
4  

Execution completed.
714645 bytecodes executed
11 thread switches
753 classes in the system (including system classes)
4221 dynamic objects allocated (218596 bytes)
10 garbage collections (105496 bytes collected)
ALERT: java/lang/ClassFormatError: Bad magic value.
Execution completed.
714645 bytecodes executed
11 thread switches
753 classes in the system (including system classes)
4221 dynamic objects allocated (218596 bytes)
10 garbage collections (105496 bytes collected)

kann mir da jemand sagen wie ich diesen Fehler beseitigen kann ich vermute es liegt an der Jar


----------

