how to import classes from another package in java - java

I have a class Student_results in the package Students_Record and I want to import that to a class in the package Forgot. I have:
package Students_Record;
public class Student_results extends javax.swing.JFrame {
and
package Forgot;
import Students_Record.Student_results;
It says "package Students_Record does not exist".
Edit.
I am using NetBeans IDE 8.2.
Both project folders are in my "NetBeans Projects" folder.
What am I doing wrong? Thanks.

On your project Forgot click right -> build path -> projects. Add your project here (i.e import Students_Record).

Your import statement is correct.
Possible cause
pre-compiled import issue.
[if using plain text editor like notepad then compile Students_Record first using javac and then compile class in Forgot.
detailed ref : Importing java classes text editor
[if using IDE like eclipse then click save all in toolbar.]
those packages might be in different projects. provide folder location as asked by #TheScientificMethod.
if you are trying to import from jar then make sure of your classpath values.
[if using plain text editor like notepad then set classpath value.]
[in case of eclipse IDE, refer build path by checking Properties
section.]
Note- Always follow standard programming conventions for any programming language. Correct your package, class names accordingly.
Reference - https://www.oracle.com/technetwork/java/codeconventions-150003.pdf,
https://www.oracle.com/technetwork/java/javase/overview/codeconvtoc-136057.html

Related

How to Start a Java Project with package declaration on Visual Studio Code?

I am learning Java on Visual Studio Code. I have installed the "Microsoft extension for Java" in it. My basic Java programs runs fine without package declaration. But I would like to package my program. How ?
Earlier I used "IntelliJ IDEA". I used to start a New Project and declare "package com.java.learn". In Visual Studio Code there is no option to create New java Project. There is an option to create Workspace but I still have the same issue.
I have two java class. "Index.java" & "InputHelper.java". Index.java is the main java file. InputHelper is a seperate class which I use in Index.java. I want to make a project and package both ( or more ) files.
Error Message:
The declared package "com.java.learn" does not match the expected package
A package is a path of subdirectories. Say your java sources are in (subdirectory of) a directory src. All sources immediately under src have the "default" package = no package declaration.
In src/com/java/learn (4 nested directories) the package com.java.learn; is expected for java sources.
In your case create a path of 3 directories: com, java, and learn the latter containing your java source.
For the rest, try to follow the coding conventions of java: class names starting with a capital like Index, variable and method names with a small letter.
In fact though Microsoft is often underestimated, I would chose a more mainstream IDE for learning java. IntelliJ IDEA (Community edition) is fine; NetBeans IDE is a clean an nice IDE too; eclipse is used very often - though a bit overdone IMHO.
I faced a similar issue, coming from Eclipse/IDEA background you find it difficult to not have a feature in your java IDE to create a new package.
Although, Joop Eggen's answer is correct that package is a path of subdirectories but you might find it tedious to create subdirectories when the number of sub packages is greater and name of sub packages is long.
You can use the below VSCode extension :
https://github.com/jiangdequan/vscode-java-saber
It is a very handy extension.It provides support for:
New: Java files(annotation/class/interface/enum/package/JSP/HTML)
Generate Getters and Getters
Copy Qualified Name
Sort Project By Name
Run Maven Goals
Generate Docs
You can try this extension.
You can use Java Projects panel to create a new project, package, class.
Also I think there's an issue in VSC 1.63.2, because a new item is created but it's not displayed in project structure until I reload VSC window.
Another option is to put right package declaration on the first line of a class file and use inline 💡light bulb button to move that class to the package it belongs.
P. S. I'm learning Java now so I could be missing something

What is the correct package name for importing MigLayout?

FYI, I'm new to Java development and Netbeans!
I have downloaded the MigLayout jar files, both core and for swing, and added their path in the NetBeans 8.2 IDE under 'Tools->Libraries->Library_Classpath'.
My problem is that I'm trying to use the API MigLayout() but every package name that I've tried to import it results in the error "package 'Package_Name' does not exist"
The following package names fail at compile time:
import MigLayout;
import net.miginfocom.MigLayout;
import net.miginfocom.swing.MigLayout;
The following package names only fail at run time:
import net.miginfocom.layout.Grid;
import net.miginfocom.swing;
import net.miginfocom.miglayout;
Links to the MigLayout forum are broken, and none of the tutorials I've seen have the import statements. All I am asking for is a link to or list of the packages needed to use MigLayout.
If MigLayout is no longer supported, it would be nice to know that as well!
Of all packages you mentioned, the following are actually working:
import net.miginfocom.swing.MigLayout;
import net.miginfocom.layout.Grid;
It is not enough to set the jar files in the library configuration of NetBeans (Tools > Libraries). You also need to set it inside your project to get your classpath working.
In the projects view, locate the directory called Libraries (JDK will be located there) and right click on it, then click on Add Library and choose the library where you defined your jar files, in your case Library_Classpath.

How to get IntelliJ IDEA 2017.2 to offer suggestions on importing the right packages to resolve symbols?

How do I get IntelliJ IDEA to offer suggestions to resolve an unresolved symbol like Visual Studio does like in the picture below?
I wanted to use Aspose Cells for Java in a console application, so I downloaded the relevant JAR file and added a reference to it like so:
File -> Project Structure -> Dependencies Tab -> + sign
I am not sure why I do not see the library reference in the External Libraries node of the treeview on the left side.
Anyway, just because I wasn't sure, I also added the JAR files I needed to a newly created folder which I named lib and added that lib folder as a library like so:
Now, when I type in some code, the intellisense reports that it cannot resolve the symbol Workbook.
So, I qualify the type Workbook with its full name like so:
com.aspose.cells.Workbook and that symbols is resolved. To resolve all the symbols, I add the import com.aspose.cells.* statement on the top and the symbols are all resolved.
I was wondering why, like Visual Studio offers a suggestion for you to add the namespace on the top, does IntelliJ IDEA 2017.2 not give me suggestions to add the relevant import statement so I don't have to type it myself or even know what the correct import statement is?
Does it have such a feature? How do I use that feature?
I think you need to move the red line(Error line).
Press alt+Enter, then choose import class or(Something like this);

How to import file from other folder in java?

I have just started learning java, so struggling in very basics. The problem i am facing currently is "cannot find symbol : class Ques". I have resolved this issue when i was accessing package from parent directory through CLASSPATH export. Now problem is i am trying to access sub-directory from sub-driectroy like this:
family.of.adam(has)/
father.java
WifeOne(sub-direc)/wifeone.java,ChildFromWifeOne.java
WifeTwo(sub-direc)/wifetwo.java,ChildFromWifeTwo.java
Now what i am trying to do is from wifetwo.java i am accessing wifeone.java. I have tried importing (wifeone)like this:
import family.of.adam.WifeOne.*;
import WifeOne.*;
In both cases it failed to import and same error occured which i mentioned above.
I have also tried solution provided in this Question but this effects classpath of WifeOne this is what i think because when i -cp method it starts showing errors related to wifeone.
I am using normal texteditor, compiling through terminal and using mac. Kindly brief me what mistake i am doing.
Assume src is your base folder(place you compile and run the program).
src/family/of/adam/FirstWife.java
if so you need to define the package startmetn the fist line of the FirstWife.java file.
package family.of.adam;
Then,If the second java file in the,
src/Main.java
In Main.java file you need to define the import statement for user the FirstWife.java class.
import family.of.adam.FirstWife;

Java library import difficulty

I'm trying to compile a small test program I have written for a raspberry pi.
The program makes use of the Pi4J library to control the piface add-on board.
What I have done so far is based on the following tutorial: http://www.savagehomeautomation.com/piface
While I can get the above example program to compile within my IDE after setting up the class paths I get compile errors with the one I have made and as far as I can tell the imports are set up in the same way.
It says that each of the following packages does not exist:
import java.io.IOException;
import com.pi4j.component.switches.SwitchListener;
import com.pi4j.component.switches.SwitchState;
import com.pi4j.component.switches.SwitchStateChangeEvent;
import com.pi4j.device.piface.PiFace;
import com.pi4j.device.piface.PiFaceLed;
import com.pi4j.device.piface.PiFaceRelay;
import com.pi4j.device.piface.PiFaceSwitch;
import com.pi4j.device.piface.impl.PiFaceDevice;
import com.pi4j.wiringpi.Spi;
I'm assuming that my problem is relatively simple but I don't really understand how import statements work and it's quite vague topic to search about. I have included some file paths if that helps.
This is where my project resides:
/home/pi/JBerries/relay
and this is where the pi4j library is:
/opt/pi4j
I hope the following image provides some of the information requested, note that the class paths are already set up:
You need to set the CLASSPATH environment variable to /opt/pi4j or the jar file therein. WIthout this the compiler is unable to know where your libary is located and will give you the errors you describe.
If you're using a project in JBerries you need to configure the classpath for the project - the screenshot shows the classpath for single-file compilations only. To edit the project config right-click the root node in the project window and select properties.

Categories

Resources