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 7631497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:22:07+00:00 2026-05-31T06:22:07+00:00

Suppose I just created a package example and have two classes inside it, Main

  • 0

Suppose I just created a package “example” and have two classes inside it, “Main” and “Helper”.

With the simplest possible compilation (e.g., $javac Main.java Helper.java) I am already able to run it fine as long as I am in the directory containing the example package, by typing this in the command line:

$java example.Main

Questions:

  1. Why would I want to set a CLASSPATH given I can already run the program? I am guessing to be able to type “$java example.Main” from any directory on my machine, but I am not sure.

  2. What happens when I type “java -cp /path/to/your/java/class/file Main” on the command line? Right now I picture there’s file containing all the different classpaths, and that command will just add another one to it. Is it the case?

  3. Is there a difference between using “CLASSPATH=/path/to/your/java/class/file” and “java -cp /path/to/your/java/class/file Main” on the command line? How come the second one has the name of the class (i.e. Main) in the end?

  • 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-31T06:22:08+00:00Added an answer on May 31, 2026 at 6:22 am
    1. Yea, pretty much. That of course assumes you have the path to java in your PATH variable
    2. -cp or -classpath adds it’s option (a string) in front of whatever is in your CLASSPATH
    3. Yes, there is a difference. Using CLASSPATH is often more convenient as you tend to set your CLASSPATH once. From then on, java Main is enough to execute the main class. With java -cp /path/to/your/java/class/file Main you have to type the -cp /path/to/your/java/class/file every time.

    That being said, both CLASSPATH and -cp or -classpath options usually contain entries pointing to directories containing java libraries used by your program, not the directory of your program itself.

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

Sidebar

Related Questions

Suppose I have two fingers touching iPhone's screen but just one is moving. TouchesMoved
Suppose a simple ActiveRecord model corresponding to a table with just two columns, id
I have created the Mac Package in order to install some of my stuff.
Suppose i have 3 java classes A , B and C I need to
I'm using a gemset per project approach. Suppose I just installed RVM, created a
Suppose we have a package called com.example1 containing a Hello class (along with other
Suppose I want to transfer just a portion of a file over FTP -
I suppose this question could just as easily be applied to PHP with regard
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

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.