javac's recognition of packages - java

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.

Related

FIle in the same Package is not Found

I am using spring framework.
I have an interface And a class In the same package.
My interface is
package soundsystem;
public interface CompactDisc{
void play();
}
My class is
package soundsystem;
import org.springframework.stereotype.*;
#Component
public class Sgtpeppers implements CompactDisc{
private String title = "A Movie";
private String artist = "The Movie is Being Played";
public void play(){
System.out.println("The CD is Played \n"+title+"\n"+artist);
}
}
On Compilation it gives me this error
Sgtpeppers.java:5: error: cannot find symbol
public class Sgtpeppers implements CompactDisc{
^
symbol: class CompactDisc
1 error
The Interface is Compiled First and the .class file is also stored the soundsystem package.
I Think there is something wrong with the javac Command.
The command i used is
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
Is this because i changed the classpath?
1) As a general rule, to compile classes, don't perform the task from the directory of a specific package.
Instead, use the root of the application source code as base to run the javac/java command.
By following this simple rule :
2)To compile all classes located in a same package, just specify the package as "source files" :
javac soundsystem/*.java
3)To compile a specific class depending on another compiled class of your source code (in the same package or not), you don't need to specify "." (that represents the current directory) in the classpath as it is the default value.
Java documentation states indeed that :
The default class path is the current directory. Setting the CLASSPATH
variable or using the -classpath command-line option overrides that
default, so if you want to include the current directory in the search
path, then you must include a dot (.) in the new settings.
But if you explicitly set the classpath with another value, the default value is not more used.
And here you did it :
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
So you should add explicitly "." in the classpath too.
From the source code root, it would give on Windows :
javac -d . -cp ".;spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java
For Unix, separator char is :, so it would give :
javac -d . -cp ".:spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java

importing a valid package is causing a java compiler error

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

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

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.)

How to compile java class (inside a package) without IDE

I'm working in windows 7, Java 7 and have following folder:
C:\..\myApplicationV
Inside this folder there are two folders with one java class each:
C:\..\myApplicationV\graphics\Circle.java
C:\..\myApplicationV\mains\UseCircle.java
Circle.java contains following code:
package graphics;
public class Circle {
public void describeCircle (){
System.out.println("A circle is round");
}
}
I've been able to compile Circle.java so therefore I have also following file:
C:\..\myApplicationV\graphics\Circle.class
UseCircle.java contains following code:
package mains;
import graphics.Circle;
class UseCircle{
public static void main (String[] args){
Circle circle = new Circle();
circle.describeCircle();
}
}
I try to compile this last one class, I place in:
C:\..\myApplicationV\mains\
and type:
javac UseCircle.java
but I'm getting following message:
UseCircle.java:3: error: package graphics does not exist
import graphics.Circle;
^
Doing some research I have found some information at:
http://docs.oracle.com/javase/tutorial/java/package/index.html
So I have solve this isuue by placing all java classes in one package and works fine. Also I have move UseCircle.java class to base folder:
C:\..\myApplicationV
And also works. The problem is when trying to use the two package. Do you know what could be wrong ?
Please specify the full package path while compiling
cd C:\..\myApplicationV\
javac mains/UseCircle.java
java mains/UseCircle
Open the command prompt in myApplicationV folder & do the following:
javac graphics\Circle.java
javac mains\UseCircle.java
java mains.UseCircle

Categories

Resources