Solved

Compilation problems.

Posted on 1998-11-16
15
217 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:kkandasa
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 1

Accepted Solution

by:
satishb earned 50 total points
ID: 1227561

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
 

Author Comment

by:kkandasa
ID: 1227562
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
 
LVL 1

Expert Comment

by:satishb
ID: 1227563
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227564
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
 

Author Comment

by:kkandasa
ID: 1227565
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227566
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
 
LVL 1

Expert Comment

by:satishb
ID: 1227567
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:satishb
ID: 1227568
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
 

Author Comment

by:kkandasa
ID: 1227569
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
 
LVL 1

Expert Comment

by:satishb
ID: 1227570

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
 

Author Comment

by:kkandasa
ID: 1227571
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
 
LVL 1

Expert Comment

by:satishb
ID: 1227572

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
 
LVL 1

Expert Comment

by:satishb
ID: 1227573


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
 
LVL 1

Expert Comment

by:satishb
ID: 1227574
HI,

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

-Satish.
0
 
LVL 4

Expert Comment

by:evijay
ID: 1227575
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 38
topping1 challenge 7 51
wordmultiple challenge 12 90
Python Assistance 7 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now