• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i couldn't run this one

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


D:\j\excercise\Test>javac Sample.java

D:\j\excercise\Test>java com.Pack.Sample
Error: Could not find or load main class com.Pack.Sample

D:\j\excercise\Test>java Sample
Error: NoClassDefinitionFoundError exception

help me though this..

thank you
 
nick woodward
Ranch Hand
Posts: 370
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to point the JVM to the classes to load using the classpath option.

you need to use -classpath "path" or -cp "path" were path is the location of the class file you are trying to run.

something like this: javac [ options ] [ source file path ] [ classes ]

basically (and i'm sure someone who knows will correct me), you have a 'classpath' which is an environmental variable that basically sets your root folder. if Sample.java compiled, for you it's probably not set (which is why your 'path' would start with D: as the root directory), but you can check by typing 'set classpath' in command prompt.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:help me though this..

Sure!

Can you share the directory structure of Sample.java? You are using a package statement, so this should be reflected in the directory structure of your source code file as well. More detailed information can be found here.

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the guy you want ^

but do read that other link, I found it helpful in relation to packages and the classpath

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to follow conventions and use lowercase for package names.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks for reply guys...

Can you share the directory structure of Sample.java?


ofcourse.
here is the directory structure..

D:\j\excercise\Test\Sample.java

after compilation

D:\j\excercise\Test\Sample.class
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:you need to point the JVM to the classes to load using the classpath option.

you need to use -classpath "path" or -cp "path" were path is the location of the class file you are trying to run.

True! If the class files are in another directory than the current directory, you'll need to use the command line option -cp to specify the root directory containing your class files. But if it is the current directory, you don't need to use the command line option -cp as the current directory is the default. More information about the java command can be found here.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:here is the directory structure..

D:\j\excercise\Test\Sample.java

after compilation

D:\j\excercise\Test\Sample.class

That's wrong!

Your package statement isFirst of all, you should change it tobecause that's according to the naming conventions (package names are always lower case), as Bear already has mentioned.

Secondly the package name should be reflected in the directory structure. Each component of the package name corresponds to a subdirectory. Your package name is com.pack, so in the directory D:\j\excercise\Test\ you should have a suibdirectory com, in the subdirectory com you'll have another subdirectory pack and in the subdirectory pack you'll have the Sample.java file. So your directory structure should look like D:\j\excercise\Test\com\pack\Sample.java

Then you should be able to compile the source code file usingAnd then after successful compilation you can run your application

Hope it helps!
Kind regards,
Roel
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your package statement isFirst of all, you should change it tobecause that's according to the naming conventions (package names are always lower case), as Bear already has mentioned.

My bad! carelessness.

Secondly the package name should be reflected in the directory structure. Each component of the package name corresponds to a subdirectory. Your package name is com.pack, so in the directory D:\j\excercise\Test\ you should have a suibdirectory com, in the subdirectory com you'll have another subdirectory pack and in the subdirectory pack you'll have the Sample.java file. So your directory structure should look like D:\j\excercise\Test\com\pack\Sample.java


you mean if i`m gonna use a package statement in a source code , should i be having a directory refleting the package statement manually before compiling?
doesn't the jdk tool take care of that?

Then you should be able to compile the source code file usingAnd then after successful compilation you can run your application


still coudn't run because i reckon i didn't create a directory structure yet.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:you mean if i`m gonna use a package statement in a source code , should i be having a directory refleting the package statement manually before compiling?
doesn't the jdk tool take care of that?

That's exactly what I mean! No, javac doesn't take care of that, javac only compiles source code files. If you would use an IDE (not recommended in your preparation), you'll notice that an IDE takes care of that task (creating the appropriate directory structure) for you.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
That's exactly what I mean!

Doesn't feel clunky?

No, javac doesn't take care of that, javac only compiles source code files.


since i felt so, i googled to find this

D:\j\excercise\Test>javac -d . Sample.java

D:\j\excercise\Test>java com.pack.Sample
number = 99



what do you think?
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a problem with putting all the files in one directory and using -d. You cannot have two files with the same name. The package system is designed to allow two classes with the same simple name, and distinguish them by their packages. So it is better for the source and class files to have the same directory structure. The -d option is intended so you can have the source files in one location and have the .class files in a different location, but with the directory structure mirrored.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
Roel De Nijs wrote:
That's exactly what I mean!

Doesn't feel clunky?

For me it definitely doesn't. Maybe it does with just one source code file, but what would you do if you have these two source code filesBoth source code files have exactly the same name (Sample.java) and as far as I know you can't have two files with the same name in the same directory

s sivaraman wrote:
D:\j\excercise\Test>javac -d . Sample.java

D:\j\excercise\Test>java com.pack.Sample
number = 99

To be honest I was aware of the option -d (and what it does). But I never have used it on a source code file which wasn't in the appropriate directory structure because that's not how you are supposed to work with source code files and packages. So I have definitely learned something new today. And that's why I awarded you a cow (although it's probably something I will never use myself)!

s sivaraman wrote:what do you think?

It's a good find. But will only work if all your classes (source code files) have different names. Otherwise you'll get into trouble and you have to create the appropriate directory structure for your source code files as well.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic