package androidjardeveloper;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class Jar_Compiler {
public static int BUFFER_SIZE = 10240;
public Manifest m;
public String folderName;
public Jar_Compiler(File archiveFile, File[] tobeJared,String FolderName)
{
this.folderName = FolderName;
try
{
createManifest();
byte buffer[] = new byte[BUFFER_SIZE];
// Open archive file
FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream,m);
for (int i = 0; i < tobeJared.length; i++)
{
if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
continue;
System.out.println("Adding " + tobeJared[i].getName());
// Add archive entry
JarEntry jarAdd = new JarEntry(FolderName + "\\"+ tobeJared[i].getName());
jarAdd.setTime(tobeJared[i].lastModified());
out.putNextEntry(jarAdd);
// Write file to archive
FileInputStream in = new FileInputStream(tobeJared[i]);
while (true)
{
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}
out.close();
stream.close();
System.out.println("Adding completed OK");
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println("Error: " + ex.getMessage());
}
}
public void createManifest()
{
try
{
Manifest manifest = new Manifest();
StringBuffer sbuf = new StringBuffer();
sbuf.append("Manifest-Version: 1.0\n");
sbuf.append("Ant-Version: Apache Ant 1.7.1\n");
sbuf.append("Created-By: 16.0-b13 (Sun Microsystems Inc.)\n");
sbuf.append("Main-Class: "+ folderName + ".Main\n");
InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
manifest = new Manifest(is);
m = manifest;
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
}
}
Now this works fine, and can even make executable jars if the Main-Class actually points to a real class (more than likely the folder name used in the exp above will not contain a static-main, its just there to complete the manifest). Anyways the result is fine. Now i tried to import the jar made by my program into netbeans and use it as a library, but it will not work. I get no errors, it just will not consider the new jar as importable? One is what my jar maker made, and the other is a jar that netbeans made of the same exact program.
Jar1 is the one made by netbeans, and it imports just fine using the following code:
import projectpuritytest.Test;
public class Main
{
public static void main(String[] args)
{
Test t = new Test();
}
}
All this will do is run Test t which i believe just preforms a system.out. SO THE IMPORTING WORKS! But this is the jar made by netbeans.
Jar2 is the jar my program made. now keep in mind the manifest files are nearly identacle in every way (besides what the main-class is), and the inside of the jars are the exact same as well. But yet Jar2 will not import into netbeans. so when i do:
public class Main
{
public static void main(String[] args)
{
Test t = new Test();
}
}
all i get is a tip saying "Create Class Test". Not "Add import for projectpuritytest.Test" as it should say? What am I doing wrong here? Is it because of this line:
InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
does it have to be something other than "UTF-8" or something? I am completely baffled by this and any help would be super helpful, and thanks in advance for any help given!
if you want to know what the manifest files difference's are, they are below:
Jar1 (made by netbeans)
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 16.0-b13 (Sun Microsystems Inc.) Main-Class: projectpuritytest.Main Class-Path: X-COMMENT: Main-Class will be added automatically by build
Jar2 (made by the program above)
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 16.0-b13 (Sun Microsystems Inc.) Main-Class: projectpuritytest.Main

New Topic/Question
Reply




MultiQuote





|