Intellij doesn't recognize package - java

i got this sample solution from my Professor, but somehow it isn't working.
IntelliJ does not recognize the packages. That's the case for every class.
When I hover over it it says:
"Package name 'spaceman.view.util' does not correspond to the file path 'view.util' "
Has anyone any idea what could be wrong?
I'm a total beginner when it comes to coding so sorry, if the question is banal.
Thank you!!

Your project spaceman is the sources root. That means anything inside a folder view/welcome in that folder is supposed to be in the package view.welcome. If you want to keep the package spaceman.view.welcome then move the whole tree into another folder spaceman inside your spaceman project directory.
Note that it's rather unusual to have the project root itself be the source root. It's way more common to have a directory like src/ be the source root (or, if you follow the Maven/Gradle convention something like src/main/java).

Related

How to move a JFrame file inside a folder to another folder in netbeans 13?

I want to use my "Final_Frame.java" in my "SampleFrameProject.java", sort of like interconnecting it by creating a "Final_Frame" object and then typing the "this.show()".
However, an error message appears, saying that "package com.toedter.components does not exist." That is the only error I've seen; I'm confident that my "Final Frame.java" is functioning well before I copy-paste it, hence I knew the issue was caused by my copy-pasting.
Btw all of the error is pointing to JCalender, and JComponents, I'm pretty sure I followed the instructions while installing them. They are working just fine before I move the "Final_Frame.java", but after I move it, it began to show errors. So is there any way for me to copy-paste it correctly? or are there any methods to call "Final_Frame.java" inside my "SampleFrameProject.java" without copy-pasting?
Thank you in advance!
package is a fundamental concept in Java. It is nothing but a namespace for the Java source file, reflecting the directory (folder) structure of your project after the src/main/java directory.
package is usually defined at the first line of the file. For example, given the directory structure from your root directory mavenproject2, the directory structure inside if it is src/main/java/com/foo/bar/FinalJava.java, then the corresponding package defined will be
package com.foo.bar;
// imports
public class FinalJava {
// the content of class
}
For your case, it would seem that you have copied com/toedter/components/FinalFrame.java from the Hotel_Management_System project (which already has defined a line package com.toedter.components in the file) to another project mavenproject2 which has a different directory structure (which is not clear in your question).
The solution would be to modify the package line according to your directory structure then it would solve the error.
Personally, I would suggest to learn some basics in Java, take some tutorials, or at least write some simple Java console applications before diving into GUI applications.

Declaring packages in eclipse

I'm new to java and eclipse and was wondering if someone could help me.
In my comp class we have a lab each week so in eclipse I created a folder called "compClass" in this file is the "src" folder and in that folder are 3 packages. lab1, lab2, lab3, one for each week we have an assignment. We're currently on assignment 3 so I'm using "lab3" package. In this package I have 2 files, "Test.java" and "MyInteger.java". These files were given to the students and we are supposed to modify them, however that's not my issue.
My issue is on those files I keep getting a compiler error that says: The declared package "" does not match the expected package "lab3" I'm not sure why this is happening as I didn't import anything. I created new java files and copy pasted the code in from a text file. I even tried typing it out by hand and got the same error. I tried dragging and dropping the "Test" and "MyInteger" files (as java files not text files) from a folder on my desktop to the "lab3" package and got the same error. How can I fix this?
P.S. On this post: https://stackoverflow.com/questions/7628686/eclipse-the-declared-package-does-not-match-the-expected-package/13444301#:~:text=java%20files%20to%20%22package%20path,All%20should%20be%20ok.&text=If%20you%20have%20imported%20an,the%20error%20till%20you%20restart.
The top answer said to find the src/prefix1 directory and right click on it. Then build path and use as source folder. If that is the answer then could someone please help me find the "src" directory I don't even really know what that is or where to find it.
Thank you!
Every Java project has a project root which can be configured using your IDE. In your case, the root is src/. If your code is inside a subfolder in the root, this folder represents the package your code belongs to, so in this case it's a package of lab3. If you want to not include any package declarations, then either place the code directly inside src or set lab3 as the root.

JavaFX: Need help understanding project hierarchy issues

My code has gotten quite large and so I've decided to build a runnable JAR to see how it's done. I probably should've tried sooner because I'm getting 2 different errors related to the project structure, first is the "no main manifest attribute error" when trying to run the JAR from command prompt. Double-clicking the JAR does nothing (Win7). The second issue is related to the FXMLLoader explained lower down.
I followed the steps here to build the JAR, which involved moving all Maven files into the JAR directory. The compiled JAR gave me the manifest error, so I followed this which adds a Maven plugin in my pom.xml file. The error might be caused by a wrong naming convention with the line <mainClass>com.primary.Drag</mainClass> where primary is the package and Drag is my Drag.java file (class) which has the main method.
Inititally I was using the default package but read that this is not recommended for larger projects, so I put all my files into "primary". Here is my current hierarchy as shown in IntelliJ:
The problem is that ever since I created the "primary" package, I can no longer even compile the program via IntelliJ, let alone build a runnable JAR. This is due by the second error I mentioned, which is java.lang.IllegalStateException: Location is not set. on this line within primary/Drag.java:
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("firstlaunch.fxml")); It used to work with the default package, but not anymore.
I tried replacing firstlaunch.fxml with /primary/firstlaunch.fxml and /resources/firstlaunch.fxml (with and without moving resources into primary package) but no luck.
3 Related Questions:
Is my project structure incorrect?
How do I reference the fxml file from the primary package?
Is this what I should write in Maven's mainClass tags? <mainClass>com.primary.Drag</mainClass>
Is my project structure incorrect?
Answer:
Your package name should be like com.primary.******
How do I reference the fxml file from theprimary package?
Answer:
Always make sure that you are trying to load firstlaunch .xml from the class which is located in same package where that xml is kept. Means class which you wrote the loading code and xml file should be in same package
Is this what I should write in Maven's mainClass tags?com.primary.Drag
Answer:
If you package name corrected to com.primary , your main class Drag will correctly added by maven

IntelliJ does not show 'Class' when we right click and select 'New'

We're creating a new project in IntelliJ and must have something wrong because when we right click on a directory, select New and then get the context menu, Java based options are not shown. Currently get things like File, some HTML options, XML options.
We have assumed so far it's something we haven't configured correctly in the Project/Module configuration. The new module we are talking about is part of a multi module project. We created it using a Maven web archetype.
Any help configuring the project nature?
Edit: The answer is basic: 'That moment when you realise somethings not working because you haven't been clicking 'Apply'... :) We had a good laugh at ourselves when we discovered this'
The directory or one of the parent directories must be marked as Source Root (In this case, it appears in blue).
If this is not the case, right click your root source directory -> Mark As -> Source Root.
This can also happen if your package name is invalid.
For example, if your "package" is com.my-company (which is not a valid Java package name due to the dash), IntelliJ will prevent you from creating a Java Class in that package.
you need to mark your directory as source root (right click on the parent directory)
and then compile the plugin (it is important )
as result you will be able to add classes and more
If you open your module settings (F4) you can nominate which paths contain 'source'. Intellij will then mark these directories in blue and allow you to add classes etc.
In a similar fashion you can highlight test directories for unit tests.
Project Structure->Modules->{Your Module}->Sources->{Click the folder named java in src/main}->click the blue button which img is a blue folder,then you should see the right box contains new item(Source Folders).All be done;
I will share another interesting point. If you try to create a package with the reserved keyword then it will be treated as a normal directory and not a package. I was having this issue where I was creating a package named import and it was converting that to a directory.
Another possible solution is that the project name is not acceptable. For example, creating a project with spaces in the name does not block the project creation but the proper sources are not marked and when those are marked manually, I still was unable to create classes. Recreating the project with hyphens (-) instead of spaces corrected the problem for me.
Make sure you are not creating a package name which is same as predefined keywords in java like enum, int, long etc.
In my case I was trying to create a class under "enum" package. As soon as I changed package name to "enums" I was able to create class in it.
Had this issue too. Invalidating Caches/Restart did the trick for me. Please upvote so the the IntelliJ folks take this more seriously. This gives the IDE a terrible UI/UX experience.
https://youtrack.jetbrains.com/issue/IDEA-203100
There is another case where 'Java Class' don't show, maybe some reserved words exist in the package name, for example:
com.liuyong.package.case
com.liuyong.import.package
It's the same reason as #kuporific 's answer: the package name is invalid.
If you just created your project, let IntelliJ finish indexing your project.
You need to mark your java directory as Source Root ,
Right Click on Java directory
Select Mark Directory as option and click on the sub menu option Source Root
Most of the people already gave the answer but this one is just for making someone's life easier.
TL;DR
You must add the test folder as source.
Right click on java directory under test
Mark it as Tests
Add src/test/java in Test Source Folders
Thats it, IntelliJ will consider them as test source.

How to generate package structure from standalone *.java source files?

I've been searching for answer for my problem but I can't find some relevant information, so I'm asking. I've directory which contains thousands of Java classes with source code (*.java files). Each of those files contains information to what package the file belongs, its classnames + code itself of course. I need to find some function of the Eclipse IDE (or maybe of another IDE) which is able to reconstruct packages under the 'src' directory based on the information in the class files and bring to me a good Java project structure so the restored packages and its classes can be easily imported into a new Java project then. Creating the structure of packages manually would take me (maybe) lot of days...
For clarification:
I have:
directory which contains: 1.java, 2.java, 3. java, n.java...
I need:
directory which will contain:
[src] -> [package_1] -> [1.java, 2.java, etc...]
...
[src] -> [package_m] -> [3.java, n.java]
I think this must be possible somehow as the each class file contains information to which package it belongs actually.
Just if you find no better solution: At least you can import all sources into a project in Eclipse and have Eclipse move each file to the right package by means of using the quick fix for each problem. You still need to press 3 keys per wrong package declaration, but it saves you from fiddling with files and folders.

Categories

Resources