importing a valid package is causing a java compiler error - java

I am getting a compiler error when compiling Order.java file even when it contains an import statement for the other packaged class. Im not entirely sure why this is happening but here is a directory tree with some files that I have:
com/my/domain/Order.java
Inside this file are the following package and imports:
package domain;
import utils.MyDate;
com/my/utils/MyDate.java
Inside this file are the following package and imports:
package utils;
Compiler error I get when compiling Order.java :
Order.java:2: error: package com.my.utils does not exist
import com.my.utils.MyDate;
^
Order.java:5: error: cannot find symbol
public MyDate orderDate;
^
symbol: class MyDate
location: class Order
Order.java:16: error: cannot find symbol
public Order(MyDate d, double amt, String c, String p, int q){
^
symbol: class MyDate
location: class Order
Order.java:24: error: cannot find symbol
public Order (MyDate d, double amt, String c) {
^
symbol: class MyDate
location: class Order
4 errors
I am still unsure how to solve this after trying form the comments. Here is some more detail.
Existing Statements in .bash_profile :
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
export CLASSPATH=${CLASSPATH}:/Users/3aCaGa/Desktop/Java-SE-8-Programs/SimplifiedDateClass/com/my
How I am trying to compile? I am going to the java file location in the directory and running command for example :
java Order.java
For more detail on the files that and their exact contents see:
https://github.com/gosem01/Java-SE-8-Programs/tree/master/SimplifiedDateClass/com/my

Your package and import statements do not match your directory structure.
Your Order.class should have:
package com.my.domain;
import com.my.utils.MyDate;
and the Utils.class:
package com.my.utils;

To compile go to the directory where you can "see" the com folder and do:
*nix/MacOS
javac -cp . com/my/domain/*.java com/my/utils/*.java
Windows
javac -cp . com\my\domain\*.java com\my\utils\*.java
Hope it helps

Related

Compiling java class with javac doesn't work

I want to compile my java class like that: javac ResultSet.java
But I get the following error:
ResultSet.java:5: error: package data does not exist
import data.Spieler;
^
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:12: error: cannot find symbol
public native Spieler[] getSpieler();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:18: error: cannot find symbol
public ArrayList<Spieler> getMeineSpieler() {
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
How can I import the spieler class? Should I set the classpath or is there a other way to fix that?
Go one directory up and then compile it with
javac data/JNIResultSet.java
Update:
Ok, your class JNIResultSet is in package model and it uses other classes in package data.
Then your compile command should be as follows:
javac -cp . model/JNIResultSet.java
The -cp . part means, that your classpath includes the current directory. This is the root of your package hierarchy. So the compiler can find the *.java files in package data and compiles them also as needed.
You see, this can be very complicated. For more classes this will be nearly unmanageable. So you should really consider to use a build system like Ant, Maven or Gradle.
use -classpath while compiling the file as
javac -classpath <path-to-dependent-classes> JNIResultSet.java
It is required only when the Spieler is not on classpath!
for more help refer javac oracle documentation

javac's recognition of packages

I have a package LMath with a class LMatrix. LMatrix has a method public LMatrix getInverse() that throws LDimensionException.
The first line in both of these files is:
package com.kavricious.LMath;
Compiling this class in jGrasp results in no problem, but if I enter
PS C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
in Windows PowerShell, the stack trace reads:
LMatrix.java:70: error: cannot find symbol
public LMatrix getInverse() throws LDimensionException{
^
symbol: class LDimensionException
location: class LMatrix
how do I tell javac to recognize members as in the same package?
C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
That should be
C:\programming\java\javaprojects> javac com\kavricious\LMath\LMatrix.java
And similarly for all other Java files: compile from the root of the package hierarchy, and name the entire path to the .java file. Then the object files will be put in the right place, and found, and the ither .java files will be compiled as necessary.

Java can't find classes in parent package

I'm not sure if this is a classpath problem, a syntax problem, or an access modifier problem. I'm trying to implement packages for the first time in Java and having with the compiler not finding classes in the parent package.
I understand there isn't any hierarchical relationship in package structures and I am explicitly importing parent package classes in the child package class.
The parent package classes' constructors are public.
I am under the impression both directories need to be on the classpath but not sure about it. Either way, I have both dirs on the classpath to be sure.
Directory Structure
home
|
|---java
|
|---src
|
|---com
|
|---inv
|
|---mail
|
|---SendMail.java
|
|---TeradataCon.java
|
|---ExcelWriter.java
CLASSPATH
(mdexter#server) /home/mdexter/java/src/com/inv/mail # echo $CLASSPATH
.:/storage/mdexter/java/lib/*:/usr/java6_64/jre/lib/*:/usr/java6_64/lib/*:/home/mdexter/java/src/com/inv/*:/home/mdexter/java/src/com/inv/mail/*
SendFile.java (stripped down)
package com.inv.mail;
import com.inv.TeradataCon;
import com.inv.ExcelWriter;
public class SendMail
{
public static void main(String[] args)
{
TeradataCon teradata = new TeradataCon(some, args, here);
ExcelWriter xls = new ExcelWriter(some, args, here);
}
}
TeradataCon.java (stripped down)
package com.inv;
public class TeradataCon
{
public TeradataCon()
{
// stuff
}
}
ExcelWriter.java (stripped down)
package com.inv;
public class ExcelWriter
{
public ExcelWriter()
{
// stuff
}
}
Error output
(mdexter#server) /home/mdexter/java/src/com/inv/mail # javac *.java
StrategyVolumes.java:3: cannot find symbol
symbol : class TeradataCon
location: package com.inv
import com.inv.TeradataCon;
^
StrategyVolumes.java:4: cannot find symbol
symbol : class ExcelWriter
location: package com.inv
import com.inv.ExcelWriter;
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
6 errors
What I have tried
import com.inv.*; (Shouldn't matter right?)
Compiled parent classes from /home/java/src/com/inv - works
Compiled mail/*.java from /home/java/src/com/inv - doesn't work
I think you've misunderstood the classpath, for starters. You don't put package directories on the classpath - you only put the root of output directories there.
I suggest you compile from the src directory, with the output going to a bin or classes directory. For example, get rid of your CLASSPATH environment variable entirely (it's rarely useful, IME - better to specify it as a command-line option where necessary) and then use something like:
/home/mdexter/java/src # javac -d ../bin com/inv/mail/*.java
Or better, compile everything together, as JB Nizet suggests:
/home/mdexter/java/src # javac -d ../bin `find . -name '*.java'`
(Or use an IDE and/or build tool.)

Unable to place an Object of java class in another class

i have two java classes in a package. I want to create one class's object into another but it gives an error message ERROR: cannot find symbol.
package pckg;
public class aa{
private String name;
public aa(){} //Constructor of aa class
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
package pckg;
public class bb{
aa obj = new aa(); //This line gives error message
public bb(){} //Constructor of bb class
}
both classes are in a same folder pckg.
ERROR Message:
D:\Java\mypack>cd..
D:\Java>cd pckg
D:\Java\pckg>set path=d:\java\jdk1.5\bin
D:\Java\pckg>javac aa.java
D:\Java\pckg>javac bb.java
bb.java:3: cannot find symbol
symbol : class aa
location: class pckg.bb
aa obj = new aa(); //This line gives error message
^
bb.java:3: cannot find symbol
symbol : class aa
location: class pckg.bb
aa obj = new aa(); //This line gives error message
^
2 errors
D:\Java\ > javac -classpath . pckg\aa.java
D:\Java\ > javac -classpath . pckg\bb.java
If you don't specify a classpath, javac doesn't know where to find the already compiled classes.
Also, classes should start with an upper-case letter in Java. And I would avoid using the same directory for the source files and class files. You'd better put your sources inside d:\Java\src and your classes inside D:\Java classes. Then, use the following command to compile everything at once:
D:\Java\ >javac -cp classes -d classes src\pckg\*.java
Your code has no problems, maybe there is a name-conflict with some other classes in your package.
Try
javac -cp . *.java
Assume that you are inside the 'pckg' directory.
#JB Nizet already answered I think.

Cannot find class in same package

I am trying to compile Board.java, which is in the same package (and directory) as Hexagon.java, but I get this error:
Board.java:12: cannot find symbol
symbol : class Hexagon
location: class oadams_atroche.Board
private Hexagon[][] tiles;
The first few lines of Board.java:
package oadams_atroche;
import java.util.LinkedList;
import java.util.Queue;
import java.io.PrintStream;
import p323.hex.*;
public class Board implements Piece{
>---//Fields
>---private int n;
>---private Hexagon[][] tiles;
The first few lines of Hexagon.java:
package oadams_atroche;
import p323.hex.*;
public class Hexagon implements Piece{
I just cannot see what I am doing wrong. Any ideas?
Thanks
I'm quite sure you're compiling from within the wrong directory. You should compile from the source root-directory, and not from within the oadams_atroches directory.
Have a look at this bash-session:
aioobe#r60:~/tmp/hex/oadams_atroche$ ls
Board.java Hexagon.java
aioobe#r60:~/tmp/hex/oadams_atroche$ javac Board.java
Board.java:12: cannot find symbol
symbol : class Hexagon
location: class oadams_atroche.Board
private Hexagon[][] tiles;
^
1 error
While if I go up one directory...
aioobe#r60:~/tmp/hex/oadams_atroche$ cd ..
... and compile:
aioobe#r60:~/tmp/hex$ javac oadams_atroche/Board.java
aioobe#r60:~/tmp/hex$
It works for me:
cd SRC_DIRECTORY
javac -cp . PACKAGE/CLASS.java
Not sure about different platforms, but using Netbeans on Windows, it's often easiest to just create a project.
If you're trying to compile from command line:
javac -cp . *.java

Categories

Resources