• Post Reply Bookmark Topic Watch Topic
  • New Topic

import in java  RSS feed

 
Sai Prabhanjan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to Java. I've started learning import statement today.

I've compiled the program

This created Test.class file in the current working directory. How should I run the class now? I tried

but I'm getting "No class def found error". If I compile this way,

it's creating directory structure, then I'm able to run through

If I can compile without directory hierarchy, why can't I execute?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai, and welcome to the Ranch!

It's the way packages work.  Writing package com.a.b means that you will find the program in directory com/a/b (or com\a\b in Windows).
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point: javac with no -d option will put the compiled class files in the same directory as the source files.  This only makes sense if your source has no package statement.  The source will compile but you won't be able to execute the compiled files until you move them to the correct directory structure.  javac -d . [source files] will create a directory structure that matches the package statement.  Then, from that same starting directory, you can execute java [package].[Class name].
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

As you will find form the Java™ Tutorials, you sh‍uld only use com or similar to start a package name when it is your own website.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!