Compilation problems.

Hello,
I have a problem compiling a java program.

I have two files.
PulseUtils.java:

   package PulseUtils;
   import      java.io.*;
   public class PulseTrace
   {
      public static int ptrace (String component_name,
                        String file_name,
                        String log_message)
      {
            // validate input parameters
            if (file_name.equals(""))
            {
               System.out.println("Invalid   file_name");
                  return -1;
            }
            System.out.println("Done");
            return 0;
      }                  
   }


Hello.java:
  class HelloWorld {
      public static void main (String args[]) {
            PulseUtils.PulseTrace trace;
            System.out.println("Hello world");
      }
  }


I have no problem compiling PulseUtils.java.
But I get the following error when compiling hello.java.
"Class PulseUtils.PulseTrace not found in type declaration"

Both java files are in the same directory.

Thanks.
kkandasaAsked:
Who is Participating?
 
satishbConnect With a Mentor Commented:

Hi,

You have to compile from one level above that of  PulseUtils directory.
i.e. if you are in PulseUtils directory do a cd .. and compile javac PulseUtils\HelloWorld.java if you are on windows or javac PulseUtils/HelloWorld.java if you are on Unix.

Let me know if you have any problems
Satish.
0
 
kkandasaAuthor Commented:
Hello,

I tried it but it did not work. Here is the directory structure
on Win NT system.

Directory: f:\javatest
Files in directory: hello.java & PulseTrace.java

Tried the following from the f:\ drive.
    javac javatest\hello.java       compilation worked.
    javac javatest\PulseTrace.java  compilation had the error
                                    described.
Any other suggestions will be greatly appreciated.


0
 
satishbCommented:
Hi,

You have to create a PulseUtils directory. Remember you are giving the package name
package PulseUtils; Now in the directory Structure it will be like this. You have a PulseUtils directory. With in that PulseUtils directory you have two files PulseTrace.java and HellowWorld.java or hello.java depending on the HelloWorld class modifier.

PulseUtils/
     PulseTrace.java
     hello.java

Assume that PulseUtils directory is in f:\

now come to f:\ and compile javac PulseUtils\*.java

It should work. I tried that and it is working for me.

Let me know if you have any problems.

Good Luck
Satish.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
heyhey_Commented:
if you need that Java can find some class with name (for example) com.mycompany.dbstuff.Main.class, you shoul afjust the CLASSPATH variable to point the directory where all the com.... stuf begins
so if this class is positioned at
d:\temp\tryit\com\mycompany\dbstuff\Main.class
you should set CLASSPATH to point to d:\temp\tryit\ directory

so if you have
f:\javatest\hello.java
f:\javatest\PulseUtils\PulseTrace.java
 
you need to set the CLASSPATH to point to f:\javatest
now java will find the PulseUtils.PulseTrace class no matter where you are when compiling (and starting) your applications.
(Of course if your CLASSPATH contains "." and you are in f:\javatest directory java will find the PulseUtils.PulseTrace class ... - starting from the current directory)


0
 
kkandasaAuthor Commented:
Hello,

I tried what you suggested and here are the results.

Directory structure: f:\PulseUtils
Files: HelloWorld.java & PulseTrace.java
compilation from f:\PulseUtils; javac HelloWorld.java gave the
same compilation error.

compilation from f:\; javac f:\PulseUtils\*.java worked. But when
I compile the files individually, javac f:\PulseUtils\HelloWorld.java fails with the same compilation error.
So, I compiled using f:\PulseUtils\*.java, tried running
java HelloWorld in the f:\PulseUtils directory. The message
"can't find class HelloWorld" comes up.

Am I missing something here.

Thanks
0
 
heyhey_Commented:
if you have
f:\PulseUtils\PulseTrace.java
f:\PulseUtils\HelloWorld.java

and both PulseTrace and HelloWorld are from package PulseUtils
1.) adjust the classpath to point to f:\ (the directory at the bottom of your package classes)
 SET CLASSPATH=%CLASSPATH%;f:\
2.1.) now you can compile from f:\
cd f:\
javac PulseUtils\*.java
2.1.) now you can compile from f:\PulseUtils
cd f:\PulseUtils\
javac *.java
3.) you can start HelloWorld from (everywhere) c:\windows
cd c:\windows
java PulseUtils.HelloWorld

you must use the full name of HelloWorld - PulseUtils.HelloWorld
java searches through the CLASSPATH, finds f:\, looks deeper for PulseUtils.HelloWorld.class
finds it and starts it

more porblems ?


0
 
satishbCommented:
Hi,

I did the same thing as you said and it is working for me. Check out the contents of the following files, if it is the same as yours then there should be some problem with your classpath.

PulseUtils\PulseTrace.java
package PulseUtils;
       import java.io.*;
       public class PulseTrace
       {
    public static int ptrace (String component_name,
    String file_name,
    String log_message)
    {
    // validate input parameters
    if (file_name.equals(""))
    {
       System.out.println("Invalid   file_name");
    return -1;
    }
    System.out.println("Done");
    return 0;
    }
       }

PulseUtils\HelloWorld.java :

class HelloWorld {
    public static void main (String args[]) {
    PulseUtils.PulseTrace trace;
    System.out.println("Hello world");
    }
      }

First create the directory PulseUtils and keep these two files in that directory. now come to the f:\ and compile using " javac PulseUtils\HelloWorld.java" or "javac PulseUtils\*.java".

Both are working for me. if you get compilation error in any case then check your classpath and include the current directory in the class path.

Good Luck
Satish.
0
 
satishbCommented:
Hi,

I did the same thing as you said and it is working for me. Check out the contents of the following files, if it is the same as yours then there should be some problem with your classpath.

PulseUtils\PulseTrace.java
package PulseUtils;
       import java.io.*;
       public class PulseTrace
       {
    public static int ptrace (String component_name,
    String file_name,
    String log_message)
    {
    // validate input parameters
    if (file_name.equals(""))
    {
       System.out.println("Invalid   file_name");
    return -1;
    }
    System.out.println("Done");
    return 0;
    }
       }

PulseUtils\HelloWorld.java :

class HelloWorld {
    public static void main (String args[]) {
    PulseUtils.PulseTrace trace;
    System.out.println("Hello world");
    }
      }

First create the directory PulseUtils and keep these two files in that directory. now come to the f:\ and compile using " javac PulseUtils\HelloWorld.java" or "javac PulseUtils\*.java".

Both are working for me. if you get compilation error in any case then check your classpath and include the current directory in the class path.

Good Luck
Satish.
0
 
kkandasaAuthor Commented:
I got it to work by playing around with the CLASSPATH environment variable. I just need to fully understand the rules
though. Here is what happens.

Existing directory & files: F:\PulseUtils\HelloWorld.java
                            F:\PulseUtils\PulseTrace.java
CLASSPATH=f:\PulseUtils
current diretory=f:\PulseUtils
    javac PulseTrace worked.
    javac HelloWorld failed.
I changed directory (i.e cd..) to f:\ and compilation worked. WHY???
The PulseTrace class in the PulseUtils package that is needed for
the compilation on HelloWorld.java is in the directory indicated by CLASSPATH. Why did I have to change the current directory f:\ for it to work?

Thanks
0
 
satishbCommented:

Hi,

You don't try to add a directory name explisitly. Try to add " . " to the end of classpath. " . " with out quotes means current directory, whatever you are in at that time. If you add a directroy name explisitly it will work only for that one.

One reason why HelloWorld.java failed when you compiled from f:\PulseUtils is it is trying to look for PulseTrace class in the directory PulseUtils from the current directory which already is PulseUtils. Since there is no directory named PulseUtils in the PulseUtils it is unable to find that out.

Now when you compile it from outside the PulseUtils directroy it is able to see the PulseUtils directory from the current directory which is f:\ now.

I hope it answers your question.

Good Luck
Satish.
0
 
kkandasaAuthor Commented:
I understand your explanation. But why would JAVA look for PulseUtils directory inside f:\PulseUtils directory when I SPECIFICALLY SET CLASSPATH TO LOOK FOR F:\PulseUtils NOT PulseUtils inside the current directory. Is it a "bug"?

That should be all the questions on this problem. Once again thank you for all the help.
0
 
satishbCommented:

Hi,

You don't try to add a directory name explisitly. Try to add " . " to the end of classpath. " . " with out quotes means current directory, whatever you are in at that time. If you add a directroy name explisitly it will work only for that one.

One reason why HelloWorld.java failed when you compiled from f:\PulseUtils is it is trying to look for PulseTrace class in the directory PulseUtils from the current directory which already is PulseUtils. Since there is no directory named PulseUtils in the PulseUtils it is unable to find that out.

Now when you compile it from outside the PulseUtils directroy it is able to see the PulseUtils directory from the current directory which is f:\ now.

I hope it answers your question.

Good Luck
Satish.
0
 
satishbCommented:


Hi,

Thats because in the HelloWorld.java file you are specifying
    PulseUtils.PulseTrace trace;

That's why it is looking for PulseTrace class in the PulseUtils directory.

Good Luck
Satish.
0
 
satishbCommented:
HI,

I am sorry, my previous comment is wrong. I assume that's because you are running
javac from the PulseUtils directory.

-Satish.
0
 
evijayCommented:
I think the solution is

set classpath=f:\;%classpath%;.

Now compile anywhere any way and it works.

Understand first how the javacompiler works.

PulseTrace.java is independent class and even it is in package PulseUtils, giving
f:\pulseUtils> javac PulseTrace.java

works since it searches for it in current directory and compiles and PulseTrace.class gets created in f:\PulseUtils directory.

Now, HelloWorld.java is in PulseUtils directory. When you compile  HelloWorld.java, the compiler searches for a file by name PulseUtils\PulseTrace.class in the classpath and the current directory (which is f:\pulseUtils !!). Obviously it fails to find it. The trick is if you set f:\ in your classpath, now, it searches for PulseUtils\PulseTrace.class in f:\ and it finds it and doesnt complain.

vijay

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.