Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Eclipse won't save source folders to JAR

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Eclipse won't save source folders to JAR

    This is something which has been dogging me since I started using Eclipse: When I save my project to a non-runnable JAR, what ends up inside it isn't in the same folders structure as what I had in the Eclipse explorer. Right now, I'm working on a fairly titanic Java programme, so for the sake of neatness I've split my packages into three separate Source Folders - Costume, Interface and Main - each of which contains several packages, each package with several classes in it. When I look at my actual project folder under Workspace, it's all neat and tidy. I have a Costume folder, an Interface folder and a Main folder. As it should be.

    When I export this to a JAR file (with the intention of importing it into Eclipse on another computer), what ends up in the JAR file is not the above, however. The source folders are completely disregarded and instead each package is put in its own folder. When I import this into another Eclipse, all of these get imported into non-Source Folders with a .class file and a .java file in them. What's worse, because I'm using the same name for several packages in different Source Folders, those packages get merged into the same folder, with all their classes jumbled together. When I was using smaller programmes with fewer Source Folders, I could just move the old packages into new source folders and they would load just fine, but in this case I don't actually remember what went where - it is a fairly large programme.

    Short of just 7zipping my entire project folder as-is, is there any way to force Eclipse to print the folder structure I've given it, rather than dumping all my packages in the root of the archive?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Eclipse won't save source folders to JAR

    Have you selected the export option, "Create directory structure for files?"

    Alternatively, have you considered using an online code repository rather than sneaker-netting your project around?

  3. #3
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Bitbucket is a good free repo that allows you to set your projects to private.

    Rather than export to JAR there is an option for export project to archive. Import java project on the other machine and it should keep your structure. At least that is how my uni material was delivered.

    I haven't done option two for a long time now, I use bitbucket to sync projects, but if I recall correctly it should suit your needs if you don't want to learn git just now.

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse won't save source folders to JAR

    Quote Originally Posted by GregBrannon View Post
    Have you selected the export option, "Create directory structure for files?"
    I don't see this option when exporting. I'm using Eclipse 4.2.1, and it just doesn't offer me this anywhere during the export process. Is it an option I need to set somewhere else? Shamefully, I haven't dug into Eclipse's settings too much but to set my preferred code style. That said, Export -> Jar File -> Next brings me to a step which includes an option called "Create source folder structure," but the option is greyed out. I've never been able to activate it, as I don't know what it depends on.

    Something I tried to do was to set each Source Folder to use a specified output folder and not the default, but all that did was cause the exporting process to throw a whole bunch of "duplicate entry" warnings. Eventually, I had to revert back to using the default.

    Quote Originally Posted by GregBrannon View Post
    Alternatively, have you considered using an online code repository rather than sneaker-netting your project around?
    I had not considered this, but it's probably not an option I want to depend on. I try to register for as few things as possible (don't have online bookmark syncing for Firefox, say) and I mostly work on the same PC. I tend to make JAR files for backup and archiving purposes, for the most part, and I like to keep those on local machines. It also seems like this should be something Eclipse can do. In fact, I'm surprised it doesn't do that by default. I teach Java at the university and take assignments from students. Ideally, I'd like them to be able to package their files and mail them to me, or bring them in on flash drives. I need to be able to load in a local copy of a programme from other people's work.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Eclipse won't save source folders to JAR

    I am not sure I understand your layout exactly, but:
    If you are trying to export something from Eclipse to be imported to Eclipse, perhaps you could export Eclipse Project?

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse won't save source folders to JAR

    Quote Originally Posted by jps View Post
    I am not sure I understand your layout exactly, but:
    If you are trying to export something from Eclipse to be imported to Eclipse, perhaps you could export Eclipse Project?
    My layout looks like this:

    Layout.jpg

    Costumes, Interface and Main need to be separated, as I have Common in two of them.

    As to exporting a project, I don't know that Eclipse makes a distinction. It has only one Export option that I can find, accessible either through the File menu or by right-clicking on a project. This allows me to "tick" the projects I want to export, pick what files and folders from the project I want to retain and go. I pick all of my Source folders, create a JAR file, and ten when I peek into the JAR itself, the source folders no longer exist and their contents have been spilled directly into the archive. It's as if I used no source folders at all and just attached packages directly to the project, which I definitely don't do.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Eclipse won't save source folders to JAR

    Jar files are typically created to distribute applications or libraries, eg class files. Thus the source is not automatically exported. When exporting you should be presented with an options window, amongst which is a checkbox that says something like "Export Java source files and resources". Is this selected when you export?

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse won't save source folders to JAR

    I don't have that option at all, likely because I'm using the wrong kind of export. In Ecplipse, the only way I could find to export anything is to right-click on a project and select Export. This gives me a list of methods in folders. The Java folder offers three options: JAR file, Javadoc, Runnable JAR file. If JAR is the wrong choice for exporting entire projects, then this explains why I can't find the option you're referring to.

    Let's suppose for a moment that I'm a completely clueless newbie using Eclipse for the first time. I've made a Java programme spread across several Source Folders and now want to export the entire project so I can import it to someone else's Eclipse. Could you explain to me how to do this, step-by-step? I suspect there's some fundamental misunderstanding that's making me do things wrong in such a way that I don't even realise it's wrong (and thus can't explain it).

    Apologies for being so clueless. Embarrassingly, I thought I knew more than this

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Eclipse won't save source folders to JAR

    Quote Originally Posted by Fazan View Post
    This allows me to "tick" the projects I want to export, pick what files and folders from the project I want to retain and go.
    What other options show on the window where you tick the projects you want to export? Perhaps post an image of that window. I am not familiar with that version of eclipse myself, but maybe if we could have a look at the options we might see something

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse won't save source folders to JAR

    Certainly. The main window looks like this:

    Window1.jpg

    Here I've marked PersonalTest (the project), all three folders (plus .settings) and a couple of files inside. You would think that "Export all output folders for checked projects" would, in fact, export all output folders for checked projects, but it does not. Granted, as they are right now, the "output folders" are set to default/bin, but I've tried messing with the class path and Eclipse still refuses to create them. It just starts complaining about duplicate entries for everything. The next screen looks like this:

    Window2.jpg

    There's a "Create source folder structure" checkbox here, but I've never in my life seen it active. Finally, the last screen looks like this:

    Window3.jpg

    As far as I'm aware, nothing in here has to do with Source Folders.

    Again, I apologise if I'm missing something simple, but this is actually my first project to ever use more than one Source Folder.

  11. #11
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Eclipse won't save source folders to JAR

    Quote Originally Posted by Fazan View Post
    The Java folder offers three options: JAR file, Javadoc, Runnable JAR file. If JAR is the wrong choice for exporting entire projects, then this explains why I can't find the option you're referring to.
    Rather than JAR try exporting to Archive File. I'm at a computer now so I can provide screenshots. This will maintain folder structure and allow you to pick and choose what folders you want.

    File > Export > General > Archive File
    - Select folders/packages you want and other misc stuff
    - followed by file location)





    Importing is just the reverse.
    Attached Images Attached Images

  12. #12
    Member
    Join Date
    Oct 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse won't save source folders to JAR

    I apologise it took me so long to respond. The message arrived at, like, 4 AM

    I tried the instructions given and they seem to work, though I won't say "perfectly." When saved as an archive, source folders do indeed become saved in their proper folders structure. When loading the project in, however, everything is put into a folder after the name of the previous project, with all source folders now being regarded as regular folders. This, however, is very easily fixable because all I have to do is right-click on a folder and go Source -> Use as source folder. This transforms it into exactly what I want. And because folders structure is preserved, I know where everything is.

    I'm sure there's some way to build a project straight out of another project, but I haven't been able to find it. Regardless, this gives me a good way to transfer projects between machines, and is how I will require my students to save their assignments. Funny that the simplest, most basic concepts of programming are so often the ones which elude me.

    Thank you kindly for your help, everyone

    *edit*
    Never mind! Solved!

Similar Threads

  1. Creating folders in Eclipse
    By felixb in forum Java IDEs
    Replies: 3
    Last Post: October 5th, 2013, 04:45 AM
  2. Eclipse 3D Game Open Source Code?
    By MitchWardle in forum Java Theory & Questions
    Replies: 0
    Last Post: March 25th, 2013, 01:45 AM
  3. Java source classes in Eclipse INDIGO
    By Sharmeen in forum Java IDEs
    Replies: 6
    Last Post: October 13th, 2012, 06:39 AM
  4. Replies: 0
    Last Post: April 18th, 2012, 04:26 AM
  5. [SOLVED] .jar file won't execute
    By pajfilms in forum What's Wrong With My Code?
    Replies: 27
    Last Post: August 3rd, 2011, 08:43 PM