Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

problem with jar-archive

PerforatedPerforated Member Posts: 19
I have a slight problem I need a solution to.

I'm experimenting a bit with creating jar-archives and so far I haven't had much success. I've created an archive containing two packages (two more archives) these packages contain all the things needed to run the program. It's all arranged like this:

package1
class1
class2
class3
class4
textfile1.txt

package2
class1 -main method in here
class2
class3



This is where the problem starts: When I try to run the program from the command prompt it tells me that it cannot find textfile1.txt and terminates. It's a FileNotFoundException to be more precise.

I've read the documentation on jar-files and I haven't found anything to help me.

I would really appreciate some help on this matter.

Comments

  • arb123arb123 Member Posts: 696
    Check the JAR structure using a ZIP application. JARs are ZIP files but use UTF-8 encoding for internal filenames by default.

    Typical methods for invoking a main are with the commands:
    [code]
    java -classpath jarname.jar package2.class1
    [/code]

    If you make a META-INF/MANIFEST.MF file, it should have an entry:
    [code]
    Main-Class: package2.class1
    [/code]

    Then it can be invoked thus:
    [code]
    java -jar jarname.jar
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • PerforatedPerforated Member Posts: 19
    : Check the JAR structure using a ZIP application. JARs are ZIP files but use UTF-8 encoding for internal filenames by default.
    :
    : Typical methods for invoking a main are with the commands:
    : [code]
    : java -classpath jarname.jar package2.class1
    : [/code]
    :
    : If you make a META-INF/MANIFEST.MF file, it should have an entry:
    : [code]
    : Main-Class: package2.class1
    : [/code]
    :
    : Then it can be invoked thus:
    : [code]
    : java -jar jarname.jar
    : [/code]
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :


    I've checked the structure of the jar-file and everything is in order, all the files are where they should be, but I still get the error:

    FileNotFoundException because it cannot find textfile1.txt even though it is in the right directory.

    I've used the export function in Eclipse to make this jar-archive and thus the file structure in the jar-archive is the same as in the project folder but it still cannot find the file...
  • VilanyeVilanye Member Posts: 684
    Try putting the .txt file in the folder that contains the package, not in the package.


    [italic][blue]Just my 2 bits[/blue][italic]

  • PerforatedPerforated Member Posts: 19
    : Try putting the .txt file in the folder that contains the package, not in the package.
    :
    :
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :

    I've tried that now, I even tried to put the files in a different folder inside the archive but that didn't work either. It does however work when I extract the archive and run the Jar-file why is that?

    And why do all my configurations work when running the application inside the IDE, it should really be no difference, it also works when run from the command prompt.
  • arb123arb123 Member Posts: 696
    Sorry, I misread your original post. How are you loading the text file?

    Try:
    [code]
    URL url = TheClassName.class.getClassLoader().findResource("package1/textfilename.txt");
    [/code]

    You can use the url to open an input stream.

    You'll have to check the code above; doing it from memory.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • PerforatedPerforated Member Posts: 19
    : Sorry, I misread your original post. How are you loading the text file?
    :
    : Try:
    : [code]
    : URL url = TheClassName.class.getClassLoader().findResource("package1/textfilename.txt");
    : [/code]
    :
    : You can use the url to open an input stream.
    :
    : You'll have to check the code above; doing it from memory.

    I've loaded the file in the following way:

    Scanner fileScan = new Scanner(new File("filename.*");

    I will try the above method however.

Sign In or Register to comment.