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

  • SEARCH
  • Home
  • 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 7912621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:39:23+00:00 2026-06-03T13:39:23+00:00

I am having two problems regarding compiling and running an Eclipse java project from

  • 0

I am having two problems regarding compiling and running an Eclipse java project from command line. This works fine when I am just running from the eclipse IDE. I tried googling but couldn’t really get the thing working. Any help is much appreciated.

Problem 1: When I try to compile from a location different from the directory where the .java file is, it throws the error “cannot read: myfile.java“. But if I migrate to this directory then it compiles.
The command that I was giving is (when in some other directory):
javac -cp C:\ABC\src\XYZ myfile.java
The command that I was giving when in XYZ directory:
javac myfile.java
This generated two .class files myfile.class and Testing_Thread.class(I guess this because I have a thread in my code)

Problem 2: After I have compiled by going to its directory, when I try to run the program, I get the error “Exception in thread “main” java.lang.NoClassDefFoundError: myfile (wrong name: XYZ/myfile.java)” even when I am trying to run from the XYZ directory. I get the same error when I try to run from some other place also.
The command that I was giving when in XYZ directory:
java myfile
The command that I was giving when in some other place:
java -cp C:\ABC\src\XYZ myfile

I am also attaching a hierarchy of my directory structure if it is of any help:
enter image description here

  • 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-06-03T13:39:24+00:00Added an answer on June 3, 2026 at 1:39 pm

    These examples assume the following source structure:

    C:\temp\compile-test\src\a\b\c\D.java
    

    Where D.java is:

    package a.b.c;
    
    public class D { }
    

    The first problem, cannot read: myfile.java, is because it is not correct to use the cp command line option to point to your source code.

    C:\temp\compile-test\src>javac -cp c:\temp\compile-test\src\a\b\c D.java
    javac: file not found: D.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    

    This should instead be the following, where javac is run from your source folder, and we can use relative paths to the source files (NOTE – javac is run from the source folder here):

    C:\temp\compile-test\src>javac a\b\c\D.java
    

    Or this, where we specify full paths to the source files, and javac can be run from anywhere (NOTE – javac is run from C:\ here):

    C:\>javac temp\compile-test\src\a\b\c\D.java
    

    Both of the above options will result in your class files being created in the same folder as the source. I.e.:

    C:\temp\compile-test\src\a\b\c\D.class
    

    For the second problem, if you try and run a class that has a package name from ‘inside’ the package, this will result in the name being wrong (NOTE – java being run from ‘inside’ the package here):

    C:\temp\compile-test\src\a\b\c>java D
    Exception in thread "main" java.lang.NoClassDefFoundError: D (wrong name: a/b/c/D)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: D.  Program will exit.
    

    To run the D class, you should be at the package ‘root’, and supply the Fully Qualified Class Name. I.e.:

    C:\temp\compile-test\src>java a.b.c.D
    Exception in thread "main" java.lang.NoSuchMethodError: main
    

    Note I get an exception as the D class doesn’t have a main method, and so cannot be run. To fix, we add a main method:

    package a.b.c;
    
    public class D {
        public static void main(String[] args) {
            System.out.println("main");
        }
    }
    

    and re-run:

    C:\temp\compile-test\src>java a.b.c.D
    main
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVC-View Model, EF Model first I am having problems with two
Having problems with the combining of two statements in a controller. Both statements work
I was having problems while intersecting two geometries, getting a TopologyException probably due to
We are having some problems deploying our WCF services into IIS7. We have two
I'm having some problems trying to perform a query. I have two tables, one
So i'm having this problem. I have two tables (Oracle), one is called Destination
Spring's DI works fine for singleton scope bean. However, regarding to prototype scope it
I am having two problems while working in Lisp and I can't find any
I seem to be having two problems: My top nav bar links don't work
I'm having problems reading in from a txt file in matlab. The txt file

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.