package stu;

class student
{

	int age;
	String name;

	public student()
	{
  	name = "Animesh";
	age = 19;
	}
	
	public void display()
	{
	System.out.println("Student name is : " + name);
	System.out.println("Age is :"+age);
	}
	
}
import stu.student;

class student1
{

	int marks;
	String sub;

	public student1()
	{
	sub = "Mathematics";
	marks =99;
	}
	

	public void display()
	{
	System.out.println("Student marks is : " + marks);
	System.out.println("subject is :"+sub);
	}
	


	public static void main(String[] args)
	{
	student s1 = new student();
	s1.display();
	student1 s2 = new student1();
	s2.display();
	}
 
}

I have got these these two class files in a Folder named 'stu'.
This folder has the path "F:\java\"

The Environment Variable is already set.

The errors which I get are related to '....class not found....'.

I cannot understand how to run this program.
Could anyone tell me the correct way to do it....so that both the files run !!!!

Well, I do know that if you do not mark the class as public (which you did not), the default visibility is package level, which means your class can only be accessed by other classes in the same package. So either mark the class public, or put the other class in the same package. Then tell me what errors you're still getting, and paste the whole error this time.

When you use package you are creating sort of folder structure from top down. Imagine that you have following structure

  • src
    • ApplicationTest
      • model
        • Student
        • Teacher
      • gui
        • StudentGUI
        • TeacherGUI
      • dbconnection
        • DatabaseMannager
        • StudentQueries
        • TeacherQueries
    • ApplicationGUI
    • MenuGUI

ApplicationTest as being placed on the top of tree structure (in this scenario) doesn't need package declaration.
However Student being placed in model folder have to have declaration as "package model;"
So based on this make correction to your application in regards of having or removing package invocation.

Now to imports. You do not need to import classes if they are on the same level in package like ApplicationTest need access to Application GUI, or StudentQueries need access to DatabaseMannager. However as soon as caller class is on different level as called class you need to use import statement in caller class know where to find called class. Therefore StudentGUI being in package gui need access in StudentQueries that are in dbconnection package have to import dbconnection.StudentQueries; I hope this help

@ BestJewSinceJC

These are the errors that I get when I run student1.java that has the main class and imports student.java, which is in the same package.
Package name : stu
Path : F:\java\

F:\java>cd stu

F:\java\stu>javac student1.java
student1.java:1: package stu does not exist
import stu.student;
           ^
student1.java:26: cannot access student
bad class file: .\student.class
class file contains wrong class: stu.student
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
        student s1 = new student();
        ^
2 errors

F:\java\stu>

I have attached 2 jpeg images.
one shows the java files in Folder 'stu'.
and the other shows the 'stu' folder in F:\java

If any change has to be made....please tell me !!!!!

@apanimesh061

You can swim all day in the Sea of Knowledge and still come out completely dry. Most people do.

~Norman Juster

I have got these these two class files in a Folder named 'stu'.

Therefore you do not need to use package or import.
If you decide to use package then both of the classes should have the same package declaration, but do not need "import" as they know of each other as they are on same level in folder structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.