Results 1 to 9 of 9
  1. #1
    bluesmooth is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Question Why is my create jar file programatically not working? Getting "Could not find or.."

    Why is my create jar file programatically not working can anybody help? So doing this does create a jar file. When I run the jar file via command line, it gives me "Could not find or load main class ...". I'm not sure exactly why. I took some code i found here: java - How to use JarOutputStream to create a JAR file? - Stack Overflow and apparently it didn't specify the main class so I specifically added it myself. am i doing something wrong? And btw, the file (that contains all the class files) do not have any directories in them. so it's essentially just one folder with a bunch of class files in there. below is the code. if anybody can like get it to work maybe try out a hello world for testing purposes and get it to work that would be so sweet. i tried and i tried and just couldn't get it to work and confused. thanks again!

    fyi I had the commons-io-2.4 jar referenced into my build path that can be downloaded here: Commons IO - Download Commons IO
    reason i guess i added to that, though i'm not sure if it was necessary or not, was because it has a file filter which can tell which files are hidden or not and thing is without it it was adding like DS_STORE(which is hidden and i guess unnecessary) to the jar. not sure if this logic is right but just letting you know.

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileFilter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.jar.Attributes;
    import java.util.jar.JarEntry;
    import java.util.jar.JarOutputStream;
    import java.util.jar.Manifest;
    import org.apache.commons.io.filefilter.HiddenFileFilter;
    
    
    
    public class jarCreation {
    	
    	public static void main(String args[]) {
    		try {
    			run();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    	
    	public static void run() throws IOException
    	{
    	  Manifest manifest = new Manifest();
    	  manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    	  final String dir = System.getProperty("user.dir");
          System.out.println("current dir = " + dir);
          manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, ".");
          manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, HelloWorld.class.getName()); //saw somewhere it was just getName. not sure if it's right tho. it doesn't work :(
    	  JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
    	  add(new File("thefullpathofthefoldercontainingalltheclasses"), target); //not sure if this is right tho. 
    	  target.close();
    	}
    
    	private static void add(File source, JarOutputStream target) throws IOException
    	{
    	  BufferedInputStream in = null;
    	  try
    	  {
    	    if (source.isDirectory())
    	    {
    	    	System.out.println("12345");
    	      String name = source.getPath().replace("\\", "/");
    	      if (!name.isEmpty())
    	      {
    	        if (!name.endsWith("/"))
    	          name += "/";
    	        JarEntry entry = new JarEntry(name);
    	        entry.setTime(source.lastModified());
    	        target.putNextEntry(entry);
    	        target.closeEntry();
    	      }
    	      for (File nestedFile: source.listFiles((FileFilter) HiddenFileFilter.VISIBLE)) {
    	    	  System.out.println("File name: " + nestedFile.getName());
    		        add(nestedFile, target);
    	      }
    	      return;
    	    }
    
    	    System.out.println("444444");
    	    JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
    	    entry.setTime(source.lastModified());
    	    target.putNextEntry(entry);
    	    in = new BufferedInputStream(new FileInputStream(source));
    
    	    byte[] buffer = new byte[1024];
    	    while (true)
    	    {
    	      int count = in.read(buffer);
    	      if (count == -1)
    	        break;
    	      target.write(buffer, 0, count);
    	    }
    	    target.closeEntry();
    	  }
    	  finally
    	  {
    	    if (in != null)
    	      in.close();
    	  }
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Rather than staring at the code, open up the generated jar and find the META-INF/manifest.mf file. Post that, as probably there is something wrong in there. When it is known what is wrong, then it is easier to pinpoint what piece of code is the source of the problem.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Please open a command prompt window,
    change directories to be in the directory with the jar file
    enter the command:
    java -jar THEJARFILENAME.jar

    copy the full contents of the command prompt window and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    bluesmooth is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Quote Originally Posted by gimbal2 View Post
    Rather than staring at the code, open up the generated jar and find the META-INF/manifest.mf file. Post that, as probably there is something wrong in there. When it is known what is wrong, then it is easier to pinpoint what piece of code is the source of the problem.
    Weird. I'm unable to open it. It says it's not a directory. :/

  5. #5
    bluesmooth is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Quote Originally Posted by Norm View Post
    Please open a command prompt window,
    change directories to be in the directory with the jar file
    enter the command:
    java -jar THEJARFILENAME.jar

    copy the full contents of the command prompt window and paste it here.
    This is what it says. "Error: Could not find or load main class ..."

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Open the jar file in a zip file utility and see what class files are present.
    Also get the contents of the manifest file and paste it here.

    Is it possible to copy the contents of the command prompt window and paste it here?
    Instead of typing a message in from memory.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    bluesmooth is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    Quote Originally Posted by Norm View Post
    Open the jar file in a zip file utility and see what class files are present.
    Also get the contents of the manifest file and paste it here.

    Is it possible to copy the contents of the command prompt window and paste it here?
    Instead of typing a message in from memory.
    I fixed it. My Main-Class was pointing to the wrong place. It was apparently in another folder. I put it all in the root folder and it worked.

    Thanks everyone for your help!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why is my create jar file programatically not working? Getting "Could not find or

    The proper fix would have been to make the main-class point to the correct 'folder'. But that requires to properly understand how packages work in Java and how that relates to the filesystem - perhaps the next time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 12-04-2013, 10:32 PM
  2. "Error cannot find symbol" "throws BadLocationException"
    By Daniel Silvester in forum New To Java
    Replies: 8
    Last Post: 07-02-2013, 02:53 AM
  3. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  4. Replies: 11
    Last Post: 08-18-2011, 11:54 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •