• Post Reply Bookmark Topic Watch Topic
  • New Topic

IO works in textpad, not in eclipse/jar  RSS feed

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this program that I've been working on for quite a while. I finally got it done and working, utilizing textpad, where it works perfect. the problem is, when I JAR it, one portion of the program stops working.

I've got a menu program set up. When the user selects an option, it passes the file information to a program that displays the html file in a JEditorPane. The HTML files and pictures are all located in a folder in the root called Documents. When ran in textpad, it works perfectly. When ran in eclipse, it says unable to locate file. And nothing happens when the Jar is ran.

I'm at the end of my rope here. I have to hand this in this monday morning. Below is a sample of the code, and the JEditorPane Program it's self.

Please help!

This is a sample of what is passed to the html display program:


This is the JEditorPane that its passed to:

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what this is supposed to mean:



but it looks sort of like it's a relative file path. If you're going to use relative paths then you must make sure you set the current working directory correctly before you run your code.

Presumably your "textpad" sets the current directory to something; when you run the jar from the command line, set the current directory to the same thing first.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running the program off of a flash drive, and I have also downloaded it to the hard drive, all with the same results. I guess you are correct that textpad is setting the relative path. So, now the guestion becomes, how can I correct the problem so that the program it's self sets the relative path, no matter the media or drive letter being utilized?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applications can't set the current working directory. You have to do that yourself before you start the application.

Or you could hard-code the full path to the file in your code. (Sounds like that won't work.)

Or you could have your program ask the user to tell you where the file is. A JFileChooser would be the thing to use for that.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not use any of these options at this point. I am totally flexible at this point in placing all needed files in the root directory, but I still run into the same problem. What do I need to enter to so that the String file ="file: .\\java_io_File.html" is successfully passed and executed.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:... you could hard-code the full path to the file in your code.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so I had something work, but it's still not right.

I copied all files onto the desktop to work with in a directory called Final. I moved the document files to the Final folder with all of the classes. I then changed the path as follows:

String file = "file:C:\\Users\\Me\\Desktop\\Final\\java_io_File.html";

I created the Jar, and it worked.

I then copied the Jar file to a thumb drive, and ran it again. It still worked.

I then deleted the Final folder off the desktop, and it failed.

Therefore, it is definately a path issue, but I'm not sure where to go next. It needs to be a hard coded, but generic path.

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really desperate here people. I'm starting to panic!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:It needs to be a hard coded, but generic path.


It looks like you understand what's meant by "hard-coding". The disadvantage of hard-coding is that when there isn't a file at the place you told the program it would be at, then things don't work right. So once you put the file name in the program, you have to make sure the file is actually there.

As for a "generic path" -- I don't understand what that means. The bottom line is that you have to tell the program where the file is. There aren't very many ways to do that and you seem to have rejected all of them. So it looks like you're out of luck unless you revisit your earlier decisions.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another issue -- sorry if this is obvious, but:

You said you were putting the jar onto a thumb drive. Presumably that's because you plan to take it to another computer to run it. Do you expect to find the input file at a particular location on that other computer? Naturally the thumb drive won't be able to access the file on your computer when it's plugged into somebody else's computer.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimately, all the files and .jar need to be placed on a cd. On some computers, the cd drive will be seen as d:. on others, the cd drive might be the e: or f: or... drive. So, that is what I'm talking about when I say a generic path.

As for the thumb drive, I was testing to see if the jar not only contained the class files, but also the document files. which it did not. But yes, the hope was that, if the jar contained the document files, like some of those I've seen before but have no clue as to how they did it, that I had reach a solution and made it movable to another computer, which failed on test.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following blast-from-the-past thread seems relevant, but I can't quite figure out how to apply it to my code:

Max Bean
Ranch Hand

Joined: Mar 09, 2006
Posts: 31

[Post New]posted Thursday, August 30, 2007 4:18 AM
Quote Report post to moderator
Suppose I create my FileInputStream like this:

in = new FileInputStream("c:/projects/study/college/test/parameters.xml");

, is there any way else to access parameters.xml without using the hardcoded-path that way?
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 271

[Post New]posted Thursday, August 30, 2007 6:12 AM
Quote Report post to moderator
If the path is relative to your application, then:

getPath()
getAbsolutePath()
getCanonicalPath()

If you want a configurable path, place a configuration file where your application knows where to find it (relatively speaking) and read the path from the configuration file.

Correlation does not prove causality.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1291

I like...
jQuery Netbeans IDE Windows

[Post New]posted Thursday, August 30, 2007 6:42 AM
Quote Report post to moderator
here is an example Code:

view plaincopy to clipboardprint?

File f1 = new File("Test.java");
String path = f1.getAbsolutePath();

FileInputStream fis = new FileInputStream(path);



is this what you looking for..

Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791

[Post New]posted Thursday, August 30, 2007 3:04 PM
Quote Report post to moderator
Relative to what might be the question. This line:
view plaincopy to clipboardprint?

File f1 = new File("..\Test.java");


will create a file relative to the current user directory. See System Properties to get the current home.

You can also make a file relative to another file or a path string:
view plaincopy to clipboardprint?

new File( homeDirectory, "..\Test.java" )
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another discovery, which might help. I opened up the .jar file, and found that the Documents folder is being included when the file is converted. This again points to a path issue. There must be a way to point to the file within the jar. I opened up a few sample jars I had kicking around and they seem to be able to load jpegs that are within the jar without user input, so there is definitely a way to do this.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:There must be a way to point to the file within the jar. I opened up a few sample jars I had kicking around and they seem to be able to load jpegs that are within the jar without user input, so there is definitely a way to do this.


Well, yes, there is. But your file isn't in the jar and nowhere in this thread did you ever say that was an option. And it didn't look to me like the file was the sort of thing you would treat as a resource. (I work in the real world and I tend to forget that teachers ask you to do things which normally wouldn't make sense, as part of the learning process.)

So, yeah, put it in the jar. Put in the same folder as the class which is going to access it, and then you can get its URL like this:


 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the solution! Thing is, I played with both the getClass().getResource() and fileURL last night, but not in this combination.

Thank you very much!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!