aspose file tools*
The moose likes Beginning Java and the fly likes Why does the class file compile in a different place Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does the class file compile in a different place" Watch "Why does the class file compile in a different place" New topic
Author

Why does the class file compile in a different place

Maha Hassan
Ranch Hand

Joined: Aug 02, 2005
Posts: 133
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
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


MV
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maha Hassan
Ranch Hand

Joined: Aug 02, 2005
Posts: 133
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

Joined: Aug 02, 2005
Posts: 133
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Aug 02, 2005
Posts: 133
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does the class file compile in a different place
 
Similar Threads
classpath error
Compiling classes at runtime
How to make this packages example program work
how to copy resources with maven?
Java Beginner