J Thompson

Ranch Hand
+ Follow
since Jan 29, 2019
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by J Thompson

Thank you for the help, I will update the previous post as well as my own code

Jack
4 years ago

Paul Clapham wrote:
You can't write to the inside of an already-created jar archive. (I think that's what you were asking.)



Ah, this is what I was trying to do. Sorry for taking so long to reply, I've been reading through the tutorials several times to understand them as best as I can.

I have fixed the original issue of the crashing. The comment made earlier about the directory being incorrect, was infact the problem. I was having issues because I was trying to access and change files that were inside the archive. You mentioned that it is not possible to write to an already existing archive, I am not sure if it is also not possible to simply access an already created archive, however I found the way to make the program work was, as mentioned earlier in the thread, to create a new folder with all the files I want to be able to change.

After making the new files etc. work correctly, I am now able to access and edit all of the files with ease. If somebody else is reading this post with similar difficulties, I continued to use FileInputStream and FileOutputStream. To read more about them I found lots of helpful information at JavaTpoint. I also used File. I did not use URL's at all.


Thank you everybody for your help. I've posted the code below for anybody who wishes to read an extra long post.

Jack


After surfing the internet looking at hundreds of different threads to fix this, and finding it difficult to write the code (its a new concept to me), I've posted the code that I used below. The coding itself is by somebody beginning at both Java and coding in general and there will be much better ways to do the same thing. In the code I am also aware that some of the names are not the best and could be confusing, I wrote the code to make it work and am now going through it again renaming. Perhaps not the best practice, but I was more focused on making it work than what I was naming stuff, now I have the task of renaming everything. Lessons learnt everyday.

For anybody reading this post and going through the code, you may see methods named 'commentStart', 'commentFinished' or 'comment' followed by a string. All these methods do is (using System.out.println) output whatever the string is. I personally like to go through the code this way when there is an issue, I find it easier to see where a potential problem is. Perhaps this is a poor way to do this, however it currently works for me.

The first batch of code I am using is to check if the folders and files have been created. The second batch of code is what I have used to get the information from the files. The final batch of code is how I am putting information into the files that have been created.







EDIT: Code updated after errors and suggestions from below
4 years ago
So I've been looking around on youtube, I have found this tutorial, along with many others


I have watched the video about half way through, and the code that has been written is the following



This code works both when the application is run inside eclipse, and once exported. Once exported and the program is run, the file is created, however is saved outside of the jar file.

My question has now become, how do I set the save location to inside the jar file, without knowing exactly where the file will be placed?
4 years ago

Paul Clapham wrote:The question also occurs to me: Why are you producing a jar which expects to find files outside the jar, but in the same directory? A common purpose of jar archives is to distribute software in a single unit, so if you turn around and say there's also a file which isn't part of that self-contained unit then something doesn't look quite right.

So, shouldn't that file be contained in the jar, as a resource? If it's read-only then that's what I would expect. But if you plan to update it then I would expect it to be elsewhere, like in the user's home directory. And the software in the jar file would be responsible for creating it in that location.



I'm not, I want to create a file that is inside the jar, and can be read from inside the jar. I'm on youtube again looking through all the tutorials again, will update if I can get it to work. Thanks, Jack
4 years ago

Paul Clapham wrote:That seems to me to be a horrendously complicated alternative, compared to the option of using the "cd" command to change the current working directory before running the jar.



I had put in a massive post here - because I thought I had fixed the issue. But I was wrong :(

Your correct, using URL was a pain in the backside. I will continue to look into how to use the cd command. Thanks, Jack
4 years ago

Knute Snortum wrote:
I'm curious how you exported the project from Eclipse.  For me when I choose Export from the File menu, the first thing it does is offer a list of choices for how to export.  Under Java is Runnable Jar.  What were you choosing if not a jar file?



I misunderstood the question, I thought you meant the files I was loading. The project itself is exported from Eclipse as a runnable jar (I get three options, JAR file, Javadoc and Runnable JAR file). I'll have a look into your idea again, thank you.

Once I have the program working correctly I will post code to update the thread and give a solution incase anybody else is looking for a solution to the same issue.

Thanks, Jack
4 years ago

Paul Clapham wrote:
Now I notice that you're using a relative path to access some files. When you run the jar, have you set your current working directory to be the same as what it is when you run the project in Eclipse?



After more testing this is what appears to be causing my issue.

I have been playing around with URL, and as you suggested, it turns out that the directory is different when the program is run in Eclipse compared to once it is exported. Anybody else having a similar issue, I've found the following post helpful in testing this. StackOverFlow URL. I will continue to play around until I have fixed the issue.

I hope its ok to link to somewhere outside of the forum, so far I haven't been told not to, if its an issue, let me know and I'll both remove the link and stop posting them.

EDIT - As I'm sure you can tell I'm new to this, I used the URL as a test, I'll play around with the program and once I've got it working correctly I'll update the post with how I fixed it - assuming thats ok

Thank you everybody for your help.

Jack
4 years ago

Knute Snortum wrote:Are you exporting to a jar file?  If so, can you use an unzip-type program to look inside the jar and see if the files you expect are in the resources directory?



I have created an editor to change the map whilst inside the program. The map is then saved using FileOutputStream. The map is loaded at the start of the program using FileInputStream. During my testing of exporting the program I have noticed that neither of these are working either, although they work fine when run inside eclipse itself, however I figure one problem at a time. When the file is saved, it is saved using FileOutputStream.write(). The file is named as a .txt file, however does not show any readable (by humans at least) information when opened. The file is saved to a class folder, /resources/maps. Inside this class folder is also where my textures are stored, and these work both, inside eclipse and once exported.

Whilst typing this I thought that perhaps this was not working because I was using a class folder instead of a source folder. I used the code from the original post, and I had the same issue, where the program loads as expected inside eclipse, but will not run once exported. I tried again with the updated code, the program now loads in both Eclipse and once exported, however the combo box is empty. Both ways work inside eclipse as expected, and the combobox has selectable items inside it.

I will look into how to export to a JAR file as well and update once I've tried it.

Thanks, Jack
4 years ago
Ok, thank you for posting for me

I'll add the catch as well thank you

Knute Snortum wrote: I don't see where the variable loadComboBoxNames is ever used to add to loadComboBox.



Sorry, that was left over code from trying different ways to get the code to work, its not used in this version

EDIT: My mistake, no its not, I tried this code first to see if it worked, which it did. When the program was run in Eclipse everything would work as expected. Once exported the program would run, however the combo box would not have any items inside it. I tried afterwards using the loadComboBoxNames variable, and the same happened, worked in eclipse but not once exported.

EDIT 2 - update...

The current code I have is the following...



I am finding that, if I run the program inside eclipse, the levelEditorCanvas has a background color of Black, which is what I was expecting, however when I export the file the background returns to the yellow color. For some reason when exported the directory stream == null?

Thanks, Jack
4 years ago
After looking more into this, I found that if I use a directory stream instead of the original code in the first post, once exported the program runs, although the items are not put into the comboBox even though when using the run method it appears correctly - I need to look into this more.

I do not understand why the program would not run with the original code? I've posted the new code below incase it helps.

Is anybody able to explain this to me?

Thanks, Jack

4 years ago
Update for anybody else reading this thread with similar issues. I have updated Eclipse to the latest build, which didn't fix my issue.

It turns out that there is a line of code causing the program to close, but I'm not entirely sure why, hopefully somebody can guide me on here. I also do not understand why the program will run with eclipse, but not once exported.

I am using a combo box to load different files that have been saved. I am attempting to make a game, and the files to be loaded are the files that store the data for the map/level (whichever you prefer to call it.

If I allow the line (and the following code that uses this) int fileLength = mapToLoad.listFiles().length; to run, the application will not run when exported. My assumption is that (being new to java) I have implemented this incorrectly, I'll have another look at the documentation. I've posted the snippet of code below that is causing my issue, if anybody has any suggestions they would be greatly appreciated.

Thanks, Jack

4 years ago
I know this is a question I should be asking on the Eclipse forum, however I don't seem to be able to create an account there at the moment. I will check again later in the day and post there as well.

I have a project that I want to export. The project runs without any issues when it is still inside eclipse itself. There are no errors when exporting either. The issue I have is that after the project has been exported (as a JAR runnable), the program will not run. I can open the file with WinZip, and everything appears to be there as well.

There are no errors when I run the export program.

I have also tried writing a quick test code that simply opens a JFrame and closes when I press the close button - which runs fine once exported.

I have tried restarting eclipse, I have also restarted my computer. I am currently re-installing eclipse itself as well incase this might be causing the issue.

Has anybody else had any issues like this? Or does anybody have some insight as to where to start looking to fix this?

Thanks, Jack
4 years ago

Paul Clapham wrote:Using plain old threads in Swing is always a recipe for grief. It would be worthwhile to read Lesson: Concurrency in Swing to find out how to do that in a happier way



Ok thank you, I'll start reading that now. As a spoiler before I finish reading, could this be the cause of the JPanel not displaying in the entire screen?

Also, I've updated the original post code so that it now has the thread inside the original class, as well as a timer instead of using an update counter.

EDIT: Just realized that I've changed the size of the JPanel so that it is larger than the JFrame. There is no reason to this, I made it larger to see if the pink line would disappear. It didn't.

Thanks, Jack
4 years ago

Paul Clapham wrote:Could you explain the purpose of that thread? It looks to me like it tries to update the display about 10,000 times and then it closes the application.

But the 10,000 repeats should take a small fraction of a second, and then everything should disappear before you can even see what happened. And I don't understand why it's in a separate thread anyway.



I am working on another project and this is the way I've implemented the thread on it. I'm trying to learn how to do what I want to do, then hopefully the process of crossing it over to the original project will be slightly easier.

I called the update to display a limited amount of times as when I run it on my PC (its an older one) it can slow it down at times, so its more of a time cap. I suppose I could have used a timer to do this as well, but as a test/learning example this felt like the quickest/easiest way to write it. I'll re-write a timer into it and update the original post.

EDIT: The original post code has been updated to get rid of the Thread class as well as to provide a timer instead of an update display count
4 years ago

Rob Camick wrote:If you need more help when you fix the above, put all the code in a single class so we can copy/paste/compile/test (if we wish) by only copying a single file.



My apologies. Changes have been made and the updated code is below. The program is meant to shut after a brief time to prevent the thread running endlessly.

I've been playing with the code, I have found that if I use the Canvas directly on the JFrame, the entire screen is filled, whereas if I use the Canvas on a JPanel, I am getting the line at the top. I have attempted to set a borderlayout to the JPanel when it is added to the JFrame, however it appears to be having no effect. I have also tried adding a borderlayout to the frame itself, but again it does not appear to be working as I was hoping...

4 years ago