Why is java telling me that my package does not exist? - java

I have two files. One is a Java file named BattleShip. The other is a JavaFX file named BattleShipJavaFX. I want to import a class from BattleShipJavaFX to BattleShip. I keep receiving this error:
For clarification, here is an image of the package name of my BattleShipJavaFX file
I have been trying to get this package to import for the past 2 hours and I'm becoming increasingly frustrated. A lot of the links I have looked to for help say they must be in the same directory. If I'm interpreting that correctly, then mine are indeed in the same directory as you can see below:
Do they mean that these files need to be located where all other packages are located (ie; Do my files need to be moved to the location where java package is stored?)? If so, where would that be located?
If not, PLEASE PLEASE PLEASE tell me what my problem is. This is my first time using JavaFX and I've only been using Java in general for 3 weeks.
Thanks in advance.

I have found the solution to my problem. Posting answer in case any other Java newbies run into this problem.
The problem was that I made two separate projects and then tried to import one project into another, when what I really needed to do was have the two files inside the same package.
As you can see from the picture below, I moved copied BattleShipJavaFX.Java and just pasted it into my battleship class. My program now works as intended.
Thanks to everyone who helped me out.

Related

VS Code Java Debugger Making a Lot of Untracked Files

I actually don't remember what I did/installed precisely, but it should be something along the line of "installing Java Debugger". This is giving me a lot of(more than one thousand) .project untracked files, even if I delete them, they pop back up again.
Edit: Just found this link: What project files does Visual Studio Code create via its Java extensions? , it seems like the question is the situation I'm in right now, but the answer is still very confusing. So what should I do?
Thank you!

How to use JWrapper graphical interface

I have a jar program and Im trying to use JWrapper to create an installer for windows and for mac. This is the first time I try this so forgive me for ths basic question, but I cant figure out how to fill in the different fields in the JWrapper interface. For example, Im asked to provide the classpath. I tried pointing to the folder which contains the jars which my program uses but that doesnt work. I am also asked to provide the path to "Files". What do they mean by that? I would appreciate any help. Thanks.

Unable to import image resources into our GUI

I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.
Here's an example of some of the references:
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))
jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));
I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?
Hope that all made sense!
For the examples you have given your file structure might look like this
src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg
For a more best practice way of organizing your resources you could do this
src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg
and then use the following calls to load them
getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")

Can I include code from one file to the next, similar to Includes in html

Ok, Please be gentle and kind. I am taking an Android Programming class in college and its our teachers first semester so he is learning as we are... Nothing against him, he's a good teacher, just doesn't know the answer.
I don't know how to put this in Android/java language so here goes...
Is there a way to use something similar to Includes, in web design, in Android. I am looking to add similar java code from one activity to the next, can i make a file that if i change this one file it will change in all the files its included it.
I know it can be done with the XML files but i couldn't find anything to show that it can be done in the actually coding... but then again i don't know the correct terminology. Please any help would be great. And thank you for taking the time to read and answer me. I apologize if this was somewhere else, I just couldn't find it.. Thanks again
Put anything you wish to "include" in Java classes, then import them. http://leepoint.net/notes-java/language/10basics/import.html
Android Programming class! I would've loved that in University...
Regardless, if you're using Eclipse for your IDE, it should pickup any changes from your java imports automatically. If you choose to refactor other java classes within your project, a rename for example (with "Update All References"), will update all references to that class across your project.
You could also use Maven (dependency management) to pull the latest third-party (or other) .jars at build time, if that's applicable to what your needs are.
:)

In Eclipse, when building an Android app I've somehow deleted the "R" class and cannot get Eclipse to generate it again

I'm new to Eclipse/Android development so I'm hoping this is something basic that I'm just doing wrong. Basically I've been playing around with developing Android apps and I was editing one earlier. I changed my main.xml file and when I went back to my class that runs the app all references to the "R" class were broken. I looked in the "gen" folder (Generated Java Files) and saw that the class was completely gone from the solution.
I had been debugging not 5 minutes before and didn't have any problems doing so. The only things I had done since debugging were moving 2 files from the "drawable-hdpi" folder to the assets folder and I had edited the "main.xml" file.
I've tried cleaning the project and restarting Eclipse but neither of those ideas worked. Any suggestions?
Check your main.xml file. If there is a fault in it (invalid XML, wrong element name, etc), then the R.java file may not be generated properly and you will have broken references.
Also make sure that the XML file itself does not have broken references (to strings, colors, and other kinds of assets).
Re-examine properly all that has changed since the last time your application was working.
I have been in your situation before. I dont have direct answer.
I assume you still learning Android (Like me) ...
The best thing is redo or rewrite the code from the beginning.
It sharpen your skill, and sometimes in the middle you find the answer.
It more quicker, Rather than staring the code and try to fix it.
It work for me, but need patient (which sometimes, I dont have it ...)
;)
I have had this problem also.
By typing an error in one of the xml files, R Class couldn't be generated.
Eclipse then is so friendly of adding an import to Android.R automagically.
This is often overlooked.
Fixing the xml-file errors will regenerate the R class. But your code is not linked anymore to your.own.namespace.R But to Android.R and there it will not find your items.
Removing the Import Android.R from the top of your file fixes this problem.
To be sure if the R class is generated, but wrongly imported, you can look under the folder "gen", there should be R.java file.
You could also try by typing your.own.namespace.R. and see if the intellisense shows your items then.

Categories

Resources