• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does the class file compile in a different place

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Sorry for posting a lot of questions lately but i still do not get it.
I have a java file, but when i compile it it creates a folder then a it places the class file in it, unlike the normal compilations where the class file and the java file are in the same place.

Another thing, when i compile using the comand promt, it only creates the class file but then when i type java nameoffile it says not class definition found but if i write using the heirarichy of folders that are present when i complie using Jcreator it works althought i do not see the folders..like when i write java nameoffolder.nameoffile

please tell me what is that cause it is quite confusing
note: that program is based on an API and it creats folder with the same names of the folders in the API

example: i wrote package firstname.lastname
then i should type java firstname.lastname.nameoffile to run!!!

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can you post your exact commands to compile and execute? And tell us if your classes have package statements at the top. Thanks!!

Here's a short tutorial on the topic. See if it answers any of your questions, too.
 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have a filename.Java; which the first line reads package com.foo.bar therefore that filename.java should be located in that folder. i.e.

C:> cd com\foo\bar\

so if you want to compile:

javac com\foo\bar filename.java

to run:

java -ea:com.foo.bar filename
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you work with packages, the directory structure where you store your source and class files must be the same as your package structure.

So if you add "package firstname.lastname;" at the top of your source file, the source and class files must be in a directory named "...\firstname\lastname".

Suppose your base directory is C:\MyProject, then your source file must be in the directory C:\MyProject\firstname\lastname. You compile and run it from the base directory:

C:\MyProject> javac firstname\lastname\MyClass.java

C:\MyProject> java firstname.lastname.MyClass

See this page in The Java Tutorial: Managing Source and Class Files
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From http://java.sun.com/docs/books/tutorial/java/interpack/managingfiles.html

Each directory listed in the class path is a top-level directory in which package directories appear. From the top-level directory, the compiler and the JVM can construct the rest of the path based on the package and the class name for the class. For example, the class path entry for the directory structure shown in the previous figure would include classes but not com or any of the directories below com. Both the compiler and the JVM construct the path name to a .class file with its full package name.


I do not understant it .

and yes the source has a package as it starts
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
tell us if your classes have package statements at the top. Thanks!!



yes it has a package "package firstname.lastname"
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say your source is here:

C:\Foo\firstname\lastname\MyClass.java

The directory path you give the compiler must match the package:

That should create firstname\lastname\MyClass.class. When you run it, give the package name, which must match the directory path

See how that all works. BTW: That's all in the tutorial linked above.
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found a simple way without setting the class path
add the jar to both C:\Program Files\Java\jre1.5.0_07\lib\ext
and C:\Program Files\Java\jdk1.5.0_07\jre\lib\ext
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that works, but it's a bad idea. If you do that, your classes will be in the classpath for every single Java application that you run.

You don't need to set the classpath. See my comments and the comments of others above. It's not difficult, you just have to be careful to make sure that you run the correct commands from the correct directory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic