Be ensure that the package folder mypackage and Main.class share the parent folder.
package mypackage;
public class Child {}
I presume that the Main class is created in default package.
public class Main {
public static void main(String []args){
mypackage.Child child=new mypackage.Child();
}
}
and your directory structure should be:
main-directory/
|
|----/mypackage/
Child.class
|
| Main.class
| Main.java
| Child.java
and to launch/load the Main issue following command,
Answer from KV Prajapati on Stack Overflowjava Main
netbeans 8 - How do i "import" a java file into another file - Stack Overflow
[JAVA] When do I need to import classes, in Main file or class files?
How do I find the java command "import"?
How To Import A Package From Another Directory
Videos
Be ensure that the package folder mypackage and Main.class share the parent folder.
package mypackage;
public class Child {}
I presume that the Main class is created in default package.
public class Main {
public static void main(String []args){
mypackage.Child child=new mypackage.Child();
}
}
and your directory structure should be:
main-directory/
|
|----/mypackage/
Child.class
|
| Main.class
| Main.java
| Child.java
and to launch/load the Main issue following command,
java Main
You have the following files:
main.java mypackage/child.java
in main.java, the first line should be:
import mypackage.child;
from the directory where main.java is, run
javac mypackage/child.java
and then:
javac main.java
mypackage should contain child.java and child.class
java main should work, because it will look for child in a subdirectory called mypackage.
A few things that you may want to try:
Make the variables public static if you only want one instance of them.
Instantiate your variables class Variables vars = new Variables(); and then call the variables from the instance vars.first_name = "First Name";
If you are using the instance method then i would suggest using Getters and Setters
This may also help you in understanding how to get the class variables better.
Doing it the static way:
public class Main {
public static void main(String[] args){
Variables.first_name = "Hello, you.";
System.out.println("first_name: " + Variables.first_name);
}
private static class Variables {
private static Scanner input = new Scanner(System.in);
public static String first_name;
public static int age;
}
}
Doing it the OOP way:
public class Main {
public static void main(String[] args) {
Variables var = new Variables();
var.setFirst_name("First Name Here");
System.out.println(var.getFirst_name());
}
private static class Variables {
private Scanner input = new Scanner(System.in);
public String first_name;
public int age;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
You need to create an instance of Variables class:
public static void main(String[] args) {
Variables variables = new Variables();
System.out.println("Please enter your first name:");
variables.first_name = variables.input.next();
System.out.println("Hi " + variables.first_name + "!");
}
So, I'm currently creating a planner as one of my first projects (just a simple console planner, tells you the date, who the owner is, and eventually the user will be able to input things on a list and read off the list.
I noticed when importing my code from IntellijIDE to Eclipse (Just wanted to try them both out so I'm giving Eclipse ago) that the IDE only flagged errors if I didn't import the needed classes in the Planner.java source file.
Here's some code so all this doesn't just sound like gibberish:
This is my Main file:
public class Main {
public static void main(String[] args) {
final String PLANNER_OWNER = Planner.getOwner();
final String CURRENT_DATE = Planner.getCurrentDate();
System.out.println("This planner belongs to " + PLANNER_OWNER + "\nToday is " + CURRENT_DATE);
System.out.println("--------------------------------");
}
} And currently my only Class is:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;
public class Planner {
private static Scanner scanner;
public static final String getOwner() {
System.out.println("Who owns this planner? ");
scanner = new Scanner(System.in);
String name = scanner.nextLine();
return name;
}
public static final String getCurrentDate() {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("EEEE");
return dateFormat.format(date);
}
} So do I only need to import in my class file or is that bad practice? Also, if there's any other bad practices in this please let me know so I can not make a habit of them! This is the first thing I've built from a blank slate without tutorial help (besides documentation and already answered quora questions lol) So any advice would more than likely be extremely helpful!
p.s. if any of this is poorly formatted, or I use the wrong terminology for things I apologize it is both late and I'm a nooby at this stuff.
Edit: BTW this actually does work 100% as intended, it was simple but super fun to figure out
Also, here are screenshots of the code if you'd prefer to look at it with proper color coding Main.java Planner.java
I thought that I had posted this question 3 days ago. However, given that I've received no responses to that earlier and that I cannot locate that post on this site, I gather that said earlier post was not recorded or was not accepted as a result of some error on my part. At any rate I am trying again.
PROBLEM: My attempt to execute the import command in a console window results in the following error message:
C:\Users\rwhoe>
C:\Users\rwhoe>import java.util.Scanner
'import' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\rwhoe>
A search for “import command” via reddit r/javahelp suggests that other users do not have this problem. What am I missing?
ABOUT ME: I am a retired systems engineer and have extensive c++ programming experience but am a total newb to anything HTML or JAVA related. I recently completed the w3schools java tutorial.
ABOUT MY PC: My PC is a hp 64-bit machine running 64-bit Windows 11. I have downloaded and installed the following 64-bit java 21 JDK app for Windows from Oracle:
openjdk-21.0.1_windows-x64_bin
I set my environmental path variable to
C:\Program Files\Java\jdk-21\bin, which successfully provides access to the java command and to the javac command.
Noob question. I made a simple project. The main method initializes a "Sort" object which has its own method called Bubblesort (which accepts an int[] ) . My folder structure is as follows
Main.java
/Sorters
/sorters
Sort.classWhat happened was, I manually made a folder called Sorters (not knowing that when you compile the sort java file with the '-d' flag, it will automatically make a folder. But instead of changing it, I decided to work with that.
However I'm not sure how to import the Sort class with the given folder structure.
Here is my Main dot java file
package Sorters.sorters;
import sorters.Sort;
public class Main{ public static void main(String[] Args){
int[] test = {2, 1, 8, 5, 18, 11, 16, 24, 12, 14, 13};
Sort s1 = new Sort();
s1.bubbleSort(test);
for(int x=0; x<test.length; x++){
System.out.print(test[x] + " , ");
}
System.out.println();
}
}
and here is my sort file
package sorters;
public class Sort{
public void bubbleSort(int[] arr){
for(int x=arr.length-2; x>=1; x=x-1){
for(int y=0; y<=x; y++){
if(arr[y] > arr[y+1]){
swap(arr, y, y+1);
}
}
}
}
private void swap(int[] arr, int Left, int Right){
int temp = arr[Right];
arr[Right] = arr[Left];
arr[Left] = temp;
}
}
What am I doing wrong? Doesn't the line "package Sorters.sorters" first navigate to the Sorters folder and then navigate to the inner sorters folder after the period symbol?