Compiling Java with multiple classes with javac - java

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

Related

Java file compile using javac unable to refer to other files in the same directory

I am trying to migrate from IDEs like Eclipse to a standalone Java environment, but I'm having problems tying together multiple files into a project.
Here is some sample code, where both files are in the same directory:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
Example.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java in an IDE, the expected output of It's working is printed, however after executing javac *.java the files seem to ignore eachother.
Here is the error that occurs when executing java App.java after it's been compiled:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
How can I compile the files in a project so that they recognise eachother?
If you running Java 11 and above, java App.java will compile App.java only.
If you need to refer Example.java, first you need to compile both java files into a directory.
Let give it named 'classes'. The command will be
javac -d classes *.java
After that, you can run it via
java -cp classes com.example.main.App. Please note that App is without .class suffix
Of course, it is advisable to use build tools like Apache Maven or Gradle to build your project if it grow larger or need other dependencies.

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.

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 do I email java files to someone and have javac compile correctly?

I have several class files in a project (residing in the default package). My teacher needs me to email him the java files only, and these files need to compile on his computer without errors. To test this out, I copied my class files to another folder and ran javac myself. I get errors when I try to compile that say package org.junit does not exist import static or.junit.Assert.*; There are other errors as well but I'm assuming they're probably related to that first one.
Most of the class files in my directory are indeed test classes that I'm using to test methods with JUNIT. What files do I need to include or what changes do I need to make so that anyone could simply use javac and compile my java files?
Here's a sample of one of my classes:
import static org.junit.Assert.*;
import org.junit.Test;
public class SetRadiusTest {
#Test
public void test() {
Circle test = new Circle(200, 0, 0);
test.setRadius(200);
assertEquals(100, test.getRadius(), 1);
test.setRadius(50);
assertEquals(50, test.getRadius(), 1);
}
}
Check the java class path and it will tell you the dependencies you need to compile your application. running javac at the command line acts differently than when it is automatically compiled by an IDE like eclipse if the class paths do not match. For commonly used jar files like JUnit your teacher should probably already have it in their environment. You should only include need to include jar files which are not part of a common configuration.

Using guava with j2objc

I'm trying to use guava with j2objc but I'm getting the error:
j2objc TestJava.java
translating TestJava.java
error: TestJava.java:1: The import com.google.common cannot be resolved
error: TestJava.java:10: Lists cannot be resolved
Translated 0 files: 2 errors, 0 warnings
while running:
j2objc Test.java
where Test.java contains:
import com.google.common.collect.Lists;
import java.util.ArrayList;
public class Test {
public static void TestMethod() {
ArrayList<String> objects = Lists.newArrayList();
objects.add(0, "Hello world");
System.out.println(objects);
}
}
I've downloaded the lastest release 0.9.5 and added it to .profile:
export PATH=$HOME/bin/j2objc-0.9.5:$PATH
What else do I need to do in order to use guava?
Thanks!
The j2objc translator uses a Java compiler as its front-end, so the same classpath and sourcepath needs to be used when translating as when compiling to Java classes. Here you'll get a similar error if you ran "javac Test.java", because the guava jar needs to be included in the classpath. The j2objc distribution includes lib/guava-jdk5.jar, so run "javac -classpath /lib/guava-jdk5.jar Test.java", and fix any problems with the directory for the guava jar if necessary. Once javac can compile it, substitute "j2objc" for "javac" using the same arguments, and it should translate fine.
You don't always have to use javac first, but whenever there are translation errors reported, it's a quick test to see whether the issue is related to a missing source or class path.
One difference from Java is that when you link the application, the -lguava flag is needed to include that library.

Categories

Resources