Compiling java project with multiple dirs and dependency hierarchy - java

Note: I can successfully compile everything else besides RegisterTest.java
I have the following project structure. I want to compile this using javac only, no build tools, no IDEs.
Each file is in a package corresponding to their directory structure, for example Drink.java is in Kassensystem/AbstractProducts which translates to package Kassensystem.AbstractProducts;
Project Structure
All abstract products implement the common interface Product which is in the Interfaces folder, so they naturally import Kassensystem.Interfaces.*. Finally the Products are the concrete classes derived from the abstract product classes, so they in turn import Kassensystem.AbstractProducts.*. Finally the Register class just calculates some prices and only depends on the Product interface. RegisterTest should test the whole system and instantiates some concrete classes and passes them to Register.
So the dependency hierarchy is as follows:
Dependency Hierarchy
I'm having trouble compiling this, how would I need to set the classpath to get the right dependency order (note I'm not using jar files)?
I tried setting the classpath such that the most abstract classes come first (Interfaces -> Abstract Classes -> Concrete Products -> Register) but it's always complaining at the import statements, that the package does not exist.
$ javac -cp "Interfaces/*.class:AbstractProducts/*.class:Products/*.class" RegisterTest.java
RegisterTest.java:3: error: package Kassensystem.Products does not exist
import Kassensystem.Products.*;
^
RegisterTest.java:4: error: package Kassensystem.Interfaces does not exist
import Kassensystem.Interfaces.*;
^
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Product
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Tomato
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class TomatoJuice
location: class RegisterTest
RegisterTest.java:10: error: cannot find symbol
double total = Register.scan(products);
^
symbol: variable Register
location: class RegisterTest
6 errors

Related

Can we create more than one package in single Java file?

Can we create more than one package in a single .java file? Like:
package parent;
package parent.child; // parent exists already from the previous command
package dada.papa.beta; // dada & papa does not exits but -> for creating beta java will automatically create dada & papa
public class b{
/* see the ReadMe.txt file
here i am just creatign packages;
*/
public static void main(String[] args) {}
}
I just wanted to practice making lots of packages and sub packages in a single go.
Error:
Unix-Box ~/making_sub_packages$ javac -d . b.java
b.java:2: error: class, interface, or enum expected
package parent.child;
^
b.java:4: error: class, interface, or enum expected
package dada.papa.beta;
^
2 errors
No, you can't do that.
The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. You can read more here:
https://docs.oracle.com/javase/tutorial/java/package/createpkgs.html

Compiling Errors in Netbeans with JFreeChart

When I run my project in Netbeans 8.1 nothing goes wrong. However, when I build it to a .jar file, there are 34 errors of missing packages and symbols all referring to JFreeChart. Couple of these errors:
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:51: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingEvent;
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:52: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingListener;
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:58: error: cannot find symbol
public class ChartDeleter implements HttpSessionBindingListener, Serializable {
symbol: class HttpSessionBindingListener
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:98: error: cannot find symbol
public void valueBound(HttpSessionBindingEvent event) {
symbol: class HttpSessionBindingEvent
location: class ChartDeleter
etc.....
My code is too long to post here (6000+ lines) and contains Java swing and some charts. Everything worked fine, but the charts made these errors appear. What's the reason for this?
"javax.servlet.http does not exist", add servletapi.jar to your classpath

"error: cannot find symbol" when building Chromium for Android with new classes

I need to create a custom OmniboxResultsAdapter (org.chromium.chrome.browser.omnibox) for Chromium for Android. If I edit existing classes, everything is fine, but if I add new classes (for example, empty MyClass with no fields or methods) and try to use it in code (e.g., MyClass myObject = new MyClass()) and try to make a build with:
chromium/src$ ninja -C out/Release chrome_public_apk
I get:
../chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java:1430: error: cannot find symbol
MyClass myObject = new MyClass();
^
symbol: class MyClass
location: class LocationBarLayout
../chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java:1430: error: cannot find symbol
MyClass myObject = new MyClass();
^
symbol: class MyClass
location: class LocationBarLayout
2 errors
ninja: build stopped: subcommand failed.
I suppose the compiler cannot find the class because of ProGuard.
What files do I need to modify to get the right result?
The java class of Chromium is declared in file src/chrome/android/java_sources.gni. You need to add the file path of MyClasst to that file like this:
"java/src/org/chromium/chrome/browser/MyClass.java"

Java: Import error within single directory?

I'm new to Java.
All of my source files (eg. TreeJPanel.java, Tree.java) are in a single directory called jview, with dependencies between them. When I try to compile with javac jview/TreeJPanel.java I get this:
jview/TreeJPanel.java:39: cannot find symbol
symbol : class Tree
location: class TreeJPanel
protected Tree tree;
^
jview/TreeJPanel.java:41: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public Tree getTree() {
^
jview/TreeJPanel.java:45: cannot find symbol
symbol : class Tree
location: class TreeJPanel
public void setTree(Tree tree) {
There's 15 similar errors. I thought I don't need explicit imports from within the same directory? What am I doing wrong? It is likely my question reveals a lack of conceptual understanding of Java - please feel free to point out. Thanks!
Was Tree.java first compiled into Tree.class? when TreeJPanel.java was trying to compile, it was searching for it.
Try compiling both files together:
javac jview/Tree.java jview/TreeJPanel.java
Cause for this is very simple just u should import the Tree Class into the TreeJpanel Class
Your code should look like this
import jview.Tree;
Class TreeJPanel { ....

cannot find symbol error while compiling a java class containing other class type object

I have a java package which contains two classes. Class A and Class B. I need to create an object of A type in class B. I don't know what is happening. Please someone help me out.
package pack;
class A
class B
I'm using JDK1.5 and tomcat and placed them in java folder in my D drive.
D:\java\jdk1.5
D:\java\tomcat
Right now, my package folder is also in above location
D:\java\pack
Below is how i am compiling my java class files.
Step 1: Compiling A.java
D:\Java\pack>set path=D:\java\jdk1.5\bin (setting up path for jdk1.5 compiler)
D:\Java\pack>javac A.java (Successfuly compiled and formed A.class)
Step 1: Compiling B.java
D:\Java\pack>javac B.java (here, i get an error message )
Below is the ERROR message
Error Message
D:\Java\pack>javac B.java
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
B.java:9: cannot find symbol
symbol : class A
location: class pack.B
A a = new A(); //creating an object of A type
^
2 errors
javac pack\A.java pack\B.java
will do the trick. The compiler has to be able to resolve everything in one invocation. If it's looking for
pack.B
then that corresponds to
pack\B.java
in the directory structure

Categories