Results 1 to 8 of 8
  1. #1
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Java problem-FileReader

    Hi, I'm new to java and I've got a few problems:
    1)I'd like to export my programs, to be able to open them in evey computer, how can I do that?(If I export it in .jar, it just doesn't open)
    2)I've written a program using FileReader to read an external text file. If i had to export my program, how would I export the text file too? I mean if I have to open the program on another computer, I won't have the same text file in the same path(now I use file reader with the path of the file), just the .jar one, so how should I do it?
    Thanks for help.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Java problem-FileReader

    1) I believe you need to associate .jar files with a Java Runtime. What happens when you double click the jar, does it ask you what program you want to use to open it with?
    Last edited by sehudson; 02-26-2014 at 05:29 PM.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,897
    Rep Power
    5

    Default Re: Java problem-FileReader

    I don't think so; I believe the OP is asking how he can make a Java program work ANYWHERE, even when there is no Java runtime to speak of.

    2) What is the purpose of that text file? Is it a configuration file perhaps?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Java problem-FileReader

    Quote Originally Posted by gimbal2 View Post
    I don't think so; I believe the OP is asking how he can make a Java program work ANYWHERE, even when there is no Java runtime to speak of.

    2) What is the purpose of that text file? Is it a configuration file perhaps?
    I see. I'm not sure that you could make the java program(.jar) run without a runtime on the system(at minimum). You would also have to have the Manifest included in the .jar, right?

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,897
    Rep Power
    5

    Default Re: Java problem-FileReader

    You can't of course - that would be like asking how you run a Windows program without the OS itself.

    You can bundle a runtime with your application and create a tidy installer package for all that, but that's anything but trivial to do right. And seriously ups the download size.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Java problem-FileReader

    Thanks for the answers. Actually that was my question, but it seems impossible te run a java application without the runtime. Well for the second question I have a text file used as a "database": on the text there are all the words of a dictionary, and I use FileReader and Scanner to read each word. At the moment my prorgam runs by using the path of the text, but if I move my jar file on another computer,I won't be able to use the .txt anymore without changing the path manually with eclipse. Is there any other way to do this?

  7. #7
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Java problem-FileReader

    It's actually relatively simple to package up a Java application together with a runtime as an exe file by using, e.g.,

    It will indeed increase the download size relatively significantly, but it shouldn't be much more that the combined size of the compiled classes + runtime. To the end user it would look like a native Windows binary.

    The path to the text file can be passed in to the Java application dynamically at runtime. There are at least 2 ways to do this:
    1. Create an external properties file that is always located in a known location, e.g., in the same folder as the Java application.
      This properties file will contain the path to the .txt file that users can adjust as needed. The location of the properties file will be hardcoded in the application, but the path to the .txt file is dynamic - as configured in the properties file. The advantages with this approach is you can add other user-configurable options into the properties file, and users can edit the properties file using, say, Notepad. See Properties (Java Platform SE 7 ).
    2. You can pass in the location of the .txt file as a command line argument.
      This is what args in public static void main(String[] args) is for. See Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment). If you choose this approach, ensure that you also provide a help text if the argument is absent or incorrect.

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,897
    Rep Power
    5

    Default Re: Java problem-FileReader

    Quote Originally Posted by Kyl9 View Post
    At the moment my prorgam runs by using the path of the text, but if I move my jar file on another computer,I won't be able to use the .txt anymore without changing the path manually with eclipse. Is there any other way to do this?
    Well of course, but what does this program of you do. Is it a command prompt application or does it have a graphical user interface using for example Swing?

    In case of command prompt:

    OPTION 1: you could not use any path at all but rather let the application assume it is in the "current working directory", which is the directory where java is executed. In Eclipse you can manually set the working directory in the run configuration. You manage this simply by using relative paths. (if that term is unfamiliar to you, I suggest you google 'relative path' and 'absolute path')

    OPTION 2: pass the path to the file as a parameter to the program

    Java Code:
    java -jar someprogram.jar c:\database\database.txt
    Java Code:
    public static void main(String[] args){
       
       if(args.length < 1){
          System.out.println("Please specify the database file to load");
          System.exit(1);
       }
    
       String pathToFile = args[0];
       ...
    }
    OPTION 3: combine both! If no file is specified, look for the file in the current working directory

    Java Code:
    String pathToFile = null;
    
    if(args.length < 1){
      pathToFile = "database.txt";
    } else {
      pathToFile = args[0];
    }
    OPTION 4: a "multi-user friendly" solution is to expect the file to be in the user's home folder. You can get that folder with System.getProperty("user.home"); Using this option has the added benefit of pretty much having a guarantee that files in that location will be write-enabled for the user.


    In case of graphical user interface:

    Well that's easy: use a file chooser component to let the user select the file to load. The options of the command prompt are also still valid.
    Last edited by gimbal2; 02-27-2014 at 09:36 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. problem with FileReader and FileWriter
    By abetemari in forum New To Java
    Replies: 20
    Last Post: 03-28-2011, 01:53 AM
  2. problem regarding FileReader
    By mukeshpaliwal in forum New To Java
    Replies: 7
    Last Post: 10-01-2010, 03:15 PM
  3. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:41 AM
  4. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 12:23 AM
  5. Help with filereader in java
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 09:55 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
  •