Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:06:10+00:00 2026-05-16T18:06:10+00:00

i have two programs one in directory /home/redhat/Documents/java1/j1 Demo1.java package j1; public class Demo1

  • 0

i have two programs
one in directory /home/redhat/Documents/java1/j1

Demo1.java

package j1;
public class Demo1
{
        public void print()
        {
                System.out.println("hi");
        }
}

and the other in directory /home/redhat/Documents/java1/j

Demo2.java

import j1.*;
public class Demo2
{
        Demo2()
        {
                Demo1 d=new Demo1();
        }
}

when i say

javac -classpath /home/redhat/Documents/java1/j1 Demo2.java

i get the following error

Demo2.java:2: package j1 does not exist
import j1.*;
^
Demo2.java:7: cannot access Demo1
bad class file: /home/redhat/Documents/java1/j1/Demo1.java
file does not contain class Demo1
Please remove or make sure it appears in the correct subdirectory of the classpath.
                Demo1 d=new Demo1();
                ^
2 errors

i want to access instance of Demo1 in Demo2
please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:06:11+00:00Added an answer on May 16, 2026 at 6:06 pm

    Your classpath is wrong. You should point to the root of any declared packages:

    javac -classpath /home/redhat/Documents/java1 Demo2.java
    

    Other previous step that I miss is the compilation of Demo1 class. Javac compiler will look for “.class” files, not “.java” ones. So before executing that you need:

    javac Demo1.java
    

    As an improvement I would suggest you that you declare your second class inside package “j” instead of default package, since it is not a good idea to have root source paths inside another root path that already contains packages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two programs, the first is a producer: public class Producer { public
I have two programs: one CLI program, and one GUI. The GUI is a
I have two begininer programs, both using the 'while' function, one works correctly, and
I'm working on an NSIS script in which I have two directory pages. One
I have two programs- First one being a C program that writes in to
I have two servers, one that runs my program written in Java (Server A)
I have two scripts that are called in one program, the first script creates
Hi I have a program written in C++ in which one or two functions
I have two programs, Writer and Reader. I have a FIFO from Writer to
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.