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.
Related
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
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
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.
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.)
I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance
Here's a concrete example of what does work, so you can compare your own situation.
Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result:
I'm MainClass
How are you building your jar file? Is the code in the appropriate package?
Does theMainClassName class have the following package line at the top:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. So if you had something like:
org/jc/tests/TestClass.class
its source file would have to look like this:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar?
Of course, it supposes the manifest points to the right class...
You should give the exact message you got, it might shed more light.
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file.
Usually this happens when a dependent class (static member) is not found - like this, using log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described.
Static constructors are another possible reason:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. I'd jar tf the jar and make sure the class is listed. I'd also double-check that the class is public and the method is "public static void main(String[] arg)".