Imported Jar Problems

My jar will not work, but netbeans jars will

Page 1 of 1

11 Replies - 1388 Views - Last Post: 31 July 2010 - 06:15 PM Rate Topic: -----

#1 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Imported Jar Problems

Posted 29 July 2010 - 06:59 PM

Well i posted a question yesterday about .class files and jars and pretty much solved what i needed and worked around the problem using suggestions by the repliers. BUT today i went to make my own jar maker and BAM! right back to a problem that makes no since to me at all! The following code is what i am using to build jars with:


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



Is This A Good Question/Topic? 0
  • +

Replies To: Imported Jar Problems

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: Imported Jar Problems

Posted 30 July 2010 - 12:38 AM

Can you also attach the two listings as files?

jar tvf your.jar >your.jar.txt

jar tvf netb.jar >netb.jar.txt

Was This Post Helpful? 0
  • +
  • -

#3 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Imported Jar Problems

Posted 30 July 2010 - 09:14 AM

View Postg00se, on 29 July 2010 - 11:38 PM, said:

Can you also attach the two listings as files?

jar tvf your.jar >your.jar.txt

jar tvf netb.jar >netb.jar.txt


I have tried to attach the jars, but the DIC Attach a File will not let me upload that file type. I get the error:

Error You aren't permitted to upload this kind of file.

but what i can do is host a .rar file with all the jar files in it on sendspace if that helps!

The link for the sendspace file is:

http://www.sendspace.com/file/oiodpr

Hopefully this will help... another thing that i did notice was the jar that my program makes (Jar2) is 2kb's smaller in size than Jar1 (Netbeans made jar). Is there some sort of signing that netbeans does that accounts for this size change? Or perhaps another process i have to put my jar through to authenticate it or something? Because once u extract the files of the .jar's, you will notice that both contents are identical.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: Imported Jar Problems

Posted 30 July 2010 - 02:39 PM

Can't open that sorry. Use jar to jar the jars
Was This Post Helpful? 0
  • +
  • -

#5 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Imported Jar Problems

Posted 30 July 2010 - 03:21 PM

View Postg00se, on 30 July 2010 - 01:39 PM, said:

Can't open that sorry. Use jar to jar the jars


Well the whole point of the project is to make a jar file via code, Of course i could use the JDK jar util, but that defeats the purpose of me making a jarring program xD. What my main question is pertains to why the jar will not open as a library. And if you just tried to run jar2 as an exe, it will not run and i did explain that above in the initial post. The mainifest file in Jar2 points to Main-Class:projectpurity or something like that, whatever the folder name inside the jar was is what the main-class is pointed to. These jars that are outputed by my program are not used to for executing, but merely used as imports for other java programs.
Was This Post Helpful? 0
  • +
  • -

#6 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Imported Jar Problems

Posted 30 July 2010 - 08:48 PM

Ok well i tried another approach and i am still stuck at the same place!

I did the following using a command prompt:
"C:\Program Files\Java\jdk1.6.0_19\bin\jar.exe" cvf Test.jar LoadImages.class *
pause



It built the jar correctly and everything, but when i import it into Netbeans as a Jar Library, i cannot call "LoadImages" at all using the following code:

import animation.LoadImages;

public class Main
{
      public static void main(String[] args)
      {
           LoadImages loader;
      }
}



and nothing... the error occurs at the "import animation.LoadImages" as if the jar full of the class files has not been imported. And i did go into the project folder, right clicked on Librarys and imported a Jar file (the "Test.jar"). I am really not understanding why the jars produced via code AND via java's JDK jar maker are not importable.
Was This Post Helpful? 0
  • +
  • -

#7 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Imported Jar Problems

Posted 30 July 2010 - 09:10 PM

The problem was the the jar painkiller made was compressed.
Was This Post Helpful? 0
  • +
  • -

#8 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Imported Jar Problems

Posted 30 July 2010 - 09:14 PM

View PostH3R3T1C, on 30 July 2010 - 08:10 PM, said:

The problem was the the jar painkiller made was compressed.


Yea to shed some more light on the issue, it was because java automatically Compresses the .class files when making a jar. So in a command line, you need to add "0" after cvf. so the line looks like this:

"C:\Program Files\Java\jdk1.6.0_19\bin\jar.exe" cvf0 Test.jar LoadImages.class *
pause



I am still not fully sure how to change the JarMaker SDK to not compress, but now that i know exactly what the issue is im sure i will find the solution for it now! Me and H3R3T1C were discussing it and he noticed that my program was outputting compressed class files... One of those things i should have read the SDK for to see if it automatically did that AND GUESS WHAT! It does xD. Thanks for everyones help for rooting out the real problem behind this conundrum!
Was This Post Helpful? 0
  • +
  • -

#9 tinaCEO  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 8
  • Joined: 30-July 10

Re: Imported Jar Problems

Posted 31 July 2010 - 01:16 AM

Well i have faced this issue. I could not do it my self. But i got a paid programmer who did this for me.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: Imported Jar Problems

Posted 31 July 2010 - 01:52 AM

I think you missed the point of my last post - i can't open whatever type of file you posted to that site. My comment was suggested as a solution to that problem
Was This Post Helpful? 0
  • +
  • -

#11 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Imported Jar Problems

Posted 31 July 2010 - 07:48 AM

View Postg00se, on 31 July 2010 - 12:52 AM, said:

I think you missed the point of my last post - i can't open whatever type of file you posted to that site. My comment was suggested as a solution to that problem


Well the way i interpreted your post was that you were trying to execute the jar file by double clicking on it. And the jars that i am producing will not work by double clicking on them (compressed or not compressed) simply because there is no main method in any of the class files. So if u were meaning Netbeans was not opening them then ya i did indeed missed your intent in your last post.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Imported Jar Problems

Posted 31 July 2010 - 06:15 PM

View Postpainkiller102, on 30 July 2010 - 09:48 PM, said:

Ok well i tried another approach and i am still stuck at the same place!

I did the following using a command prompt:
"C:\Program Files\Java\jdk1.6.0_19\bin\jar.exe" cvf Test.jar LoadImages.class *
pause



It built the jar correctly and everything, but when i import it into Netbeans as a Jar Library, i cannot call "LoadImages" at all using the following code:

import animation.LoadImages;



and nothing... the error occurs at the "import animation.LoadImages" as if the jar full of the class files has not been imported. And i did go into the project folder, right clicked on Librarys and imported a Jar file (the "Test.jar"). I am really not understanding why the jars produced via code AND via java's JDK jar maker are not importable.

Normal
your jar command for what I see is jaring the directory LoadImages but your java import statement is using animation.LoadImages

You will have to cd .. one level higher and

jar.exe" cvf Test.jar animation\LoadImages.class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1