javac file with dependency - java

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.

Related

Compilation with javac fails, with eclipse-compiler it works

I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed

Compiling Java with multiple classes with javac

Until now I always used an IDE to write my java apps. Now for different reasons, mainly also to understand more about the language and the things the IDE is just making for you, I switched to vim and have now the following problem:
I wrote two classes:
package seedInformatik.LinearList;
import seedInformatik.LinearList.*;
// A linear list. Contains LinearListElement<T>
public class LinearList<T> {
and
package seedInformatik.LinearList;
public class ListElement<T> {
Now I want to compile LinearList and get the following:
➜ LinearList javac LinearList.java
LinearList.java:10: error: cannot find symbol
private ListElement<T> first; // head of list
^
Both classes are in the same dir. What do I miss?
Many Thanks
Robin
Maybe this can help:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If your files are under some package you need to specify the package like this:
javac com.mypackage/.*java
java com.mypackage.MyClass
I would also recommend using a build tool like maven: http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project

Java Compiling RMI Server/Client Application in Multiple Packages

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.

Not able to run a simple doclet program : package com.sun.javadoc does not exist

I am trying to run a simple doclet program, but I am not able to compile it.
javac -cp /cygdrive/c/Progra~2/Java/jdk1.8.0_65/lib/tools.jar A.java
But it throws
A.java:1: error: package com.sun.javadoc does not exist import
com.sun.javadoc.ClassDoc;
Where A.java is
import com.sun.javadoc.ClassDoc;
public class A {
}
I referred it from
http://download.java.net/jdk7u2/docs/technotes/guides/javadoc/doclet/overview.html
I know that I am doing a simple mistake but I ma not able to figure it out.
Can anyone please point me out what am I doing wrong
You need to add Tools.jar to the project path. It is not included in the standard installation.
Can I ask why you need com.sun.javadoc? It is in most cases discouraged to use com.sun overall.

How does the terminal compile java programs when $CLASS_PATH is not set? [duplicate]

I'm trying to look under the hood about java compilation. So I put my IDE away and started using MS-DOS command-line...
I created a simple project, as described in the tree below :
SampleApp
|____**src**
|_____pack
|______Sample.java
|____**classes**
This is the Sample.java source code :
public class Sample
{
private String s = new String("Hello, world");
public Sample(){
System.out.println(s);
}
}
I just want to compile this class, so I used the javac command :
prompt\SampleApp\src>javac -d ..\classes -sourcepath . pack\Sample.java
All works fine, but i didn't expect that because I deleted my CLASSPATH environment variable before compiling my Sample.java file. So I was expecting a compiler error due to the fact that the compiler would not be able to find the java.lang.String class file.
I read this article http://www.ibm.com/developerworks/java/library/j-classpath-windows/ which helped me understand many things. The article author says that the default classpath is the current working directory. But I don't understand why my source code compile without error. Could someone explain this to me?
So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.
The short answer is that the compiler knows where to find all of the standard Java SE library classes without you telling it.
The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.
The classpath variable doesn't do what you think. To cite the oracle documentation:
The CLASSPATH variable is one way to tell applications, including the
JDK tools, where to look for user classes. (Classes that are part of
the JRE, JDK platform, and extensions should be defined through other
means, such as the bootstrap class path or the extensions directory.)
Source: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
Basically since java.lang.* is part of the platform and delivered with the JDK/JRE, the compiler doesn't have to be told by you where to look for them.

Categories

Resources