Java Compiling RMI Server/Client Application in Multiple Packages - java

I'm having a bit of trouble with compiling a Java RMI assignment in packages with the command line interface on Windows 10 (can't use Eclipse or any other IDE). I need to compile and run two .java files - RemoteBankServer.java, and RemoteBankClient.java. Both of these files use other .java classes (Bank.java, RemoteBankImpl.java, RemoteBank.java, Account.java and subclasses of Account.java).
Previously, with all of my files in one folder, I could easily compile and run my server and client with two command line windows, with:
javac RemoteBankServer.java
java RemoteBankServer
and the same for RemoteBankClient.
Now that I've tested that my code works fine, I need to organize them into java packages, following this structure:
- package name: edu.btp400.w2017.client
class name: RemoteBankClient
- package name: edu.bt400.w2017.server
class names: RemoteBankImpl, RemoteBankServer
- package name: edu.btp400.w2017.common
class name: RemoteBank (and all other classes, like Bank, Account, etc.)
I've created those folders (edu/btp400/w2017/) and have placed the appropriate classes in each folder, but I can't get it to compile. For now, I have these two lines at the top of each .java file:
package edu.btp400.w2017.server; //or .common or .client
import edu.btp400.w2017.*;
But I'm still getting compilation errors when trying to compile RemoteBankServer.java (in the server folder), such as this (which is weird, because RemoteBankImpl is in the same folder and RemoteBankServer:
RemoteBankServer.java:40: error: cannot find symbol
RemoteBankImpl b1 = new RemoteBankImpl();
^
symbol: class RemoteBankImpl
location: class RemoteBankServer
as well as similar errors saying that Account and other classes cannot be found. I've looked at various posts on how to compile with Java packages using the command line interface, but I haven't gotten it working. Any help would be greatly appreciated. Sorry for the long post.
Edit: this is the command I used to compile above (after storing the files in various packages):
I tried this in the folder containing edu, and got 27 compilation errors:
javac -cp . edu/btp400/w2017/server/RemoteBankServer.java
and this inside the server folder, and got 15 compilation errors:
javac RemoteBankServer.java
Edit: Thanks to user EJP, I found out that I needed to include the subfolders in my import statements: instead of just import edu.btp400.w2017.*; I needed to do:
import edu.btp400.w2017.common.*;
import edu.btp400.w2017.server.*;
import edu.btp400.w2017.client.*;

import edu.btp400.w2017.*;
That doesn't import all the classes in the subpackages indicated by *. The asterisk only refers to classes.
You need to import all the packages individually.

Related

javac file with dependency

Sorry to ask a quite common question but I can't find a working answer.
I just want to compile using javac several source files organized like this:
AdaptiveHuffmanCoding/
Encoder.java
Decoder.java
FullBinaryTree/
Node.java
Tree.java
For example, if I run
javac FullBinaryTree/Node.java
it works OK the class file is produced.
But if I run
javac FullBinaryTree/Tree.java
it will fail, reporting every Node appearance with an unknown symbol error.
As you can see the 2 files are in the same package so I'm not using any import and they are sharing the same 1st line namely
package AdaptiveHuffmanCoding.FullBinaryTree;
I guess I have to tell the compiler where to find this Node but I'm actually struggling with it. If someone could explain.
Thanks
As you can see the 2 files are in the same package...
To make classes of the same package accessible by the compiler, don't execute javac directly from this package but do it from the upper lever.
To compile the Tree class :
javac AdaptiveHuffmanCoding/FullBinaryTree/Tree.java
To compile all classes of this package :
javac AdaptiveHuffmanCoding/FullBinaryTree/*.java
Note that to be compliant with Java naming conventions : packages should not contain any uppercase characters.

How to import file from other folder in java?

I have just started learning java, so struggling in very basics. The problem i am facing currently is "cannot find symbol : class Ques". I have resolved this issue when i was accessing package from parent directory through CLASSPATH export. Now problem is i am trying to access sub-directory from sub-driectroy like this:
family.of.adam(has)/
father.java
WifeOne(sub-direc)/wifeone.java,ChildFromWifeOne.java
WifeTwo(sub-direc)/wifetwo.java,ChildFromWifeTwo.java
Now what i am trying to do is from wifetwo.java i am accessing wifeone.java. I have tried importing (wifeone)like this:
import family.of.adam.WifeOne.*;
import WifeOne.*;
In both cases it failed to import and same error occured which i mentioned above.
I have also tried solution provided in this Question but this effects classpath of WifeOne this is what i think because when i -cp method it starts showing errors related to wifeone.
I am using normal texteditor, compiling through terminal and using mac. Kindly brief me what mistake i am doing.
Assume src is your base folder(place you compile and run the program).
src/family/of/adam/FirstWife.java
if so you need to define the package startmetn the fist line of the FirstWife.java file.
package family.of.adam;
Then,If the second java file in the,
src/Main.java
In Main.java file you need to define the import statement for user the FirstWife.java class.
import family.of.adam.FirstWife;

Java Package Compiling Issue

OK, like many of the package compiling issues out there I have not found one like this out of the 12 hours I've spent searching..
Basically I have the normal setup :
My Directories are as follows: JavaCSVReader/FRC_API
My Source files are setup as this:
JavaCSVReader/CSVFile.java
JavaCSVReader/FRC_API/RobotConfig.java
(that is CSVFile.java is located in JavaCSVReader and same for RobotConfig.java)
CSVFile.java contains the lines:
package JavaCSVReader;
import JavaCSVReader.FRC_API.*;
...
RobotConfig.java contains the lines:
package JavaCSVReader.FRC_API;
import JavaCSVReader.CSVFile;
...
Both files compile fine without the lines above.
The error is thus: I receive
"cannot find symbol... class: CSVFile location: JavaCSVReader"
when I try to compile the RobotConfig.java.
I also receive the
"package does not exist: JavaCSVReader.FRC_API" error when compiling CSVSFile.java
my
CLASSPATH=/home/src/JavaCSVReader/:.:..
(I am using linux)
Your classpath setting is wrong. You should set it to
/home/src/
The compiler will take classpath as the "base" directory in order to find packages defined in the source.
it's a classpath issue.
How do you configure java environment variables?
You'd better check on it.
is your package JavaCSVReader.FRC_API; or JavaCSVReader ?

error package org.python.util does not exist, compilation with ant

I am trying to call a python method in java, similar functionality as Unresolved import org.python / working with jython and java?
However, I am using ant to compile and run my files. using import org.python.util will give me this error
package org.python.util does not exist
I can see that python.org.util exists in jython-2.5.0.jar.
So, here is the class path I have in my build.xml ant file:
classpath="${java.class.path}:./jgrapht/lib/jgrapht-jdk1.5.jar:\
./jgrapht/lib/jgraph.jar:./jgraphx/lib/jgraphx.jar:\
./jython/lib/jython-2.5.0.jar:./colt/lib/colt.jar:."
and I also I added the path to jython jar files to my class path. i.e. looks like echo $path gives me all the required paths. Is there anything missing here that I am not aware of?
Try this to make all classes in the package available:
import org.python.util.*;
Or this to include a particular class:
import org.python.util.TemplateAntTask;
EDIT: Also, looks like there is an extra slash after jython-2.5.0.jar in your classpath.

Problem with the import of packets in antlrworks

Have to create a package defined by me, containing some of the classes, and I recall that package in a file .java created of the program AntlrWorks in which i did the import. Package named "com.project.redfox" . I compiled the code with the command: "javac Test.java provaParser.java provaLexer.java" but I get the error that not exist the package.
In the grammar have added :
grammar prova;
#hader{
import com.project.redfox;
}
....something......
I created the package "com.project.redfox" within of the project redfox developed in NetBeans, therefore the directory com/project/redfox is in the directory redfox.
how can I solve this problem?
To formally answer your question: javac can't find the package com.wikirates which you are probably using in com.project.redfox.
Note that I assumed redfox is a class. If it's a package, you need to import all classes from it like this: import com.project.redfox.*; instead of import com.project.redfox; (assuming that there are classes in com.project.redfox...).

Categories

Resources