javac compilation - code in multiple folders - java

I have been sitting on it for a while and can't figure it out, although I think it's quite easy...
I have to compile the following program using javac (the program has one class and one testing class):
a class is in folder ./src/cplx/
a testing class is in folder ./test/cplx/
junit lib is in ./lib
and:
classes should be built to ./build/slasses
testing classes should be build to ./build/test
Please help me with writing a proper javac command to compile the code.
I used the suggested command end het the following error, it looks like test class doesn't see the class i have built?
amaltea:testowanie/zad1% javac -d ./build/classes ./src/cplx/*.java
amaltea:testowanie/zad1% javac -classpath ./lib/junit-4.8.2.jar -d ./build/test ./test/cplx/*.java
./test/cplx/ComplexTest.java:20: cannot find symbol
symbol : class Complex
location: class cplx.ComplexTest
Complex a = new Complex(1.1, 2.2);
^
./test/cplx/ComplexTest.java:20: cannot find symbol
symbol : class Complex
location: class cplx.ComplexTest
Complex a = new Complex(1.1, 2.2);
^
2 errors
amaltea:testowanie/zad1%

You can specify only one root destination directory with javac. If you want the root itself to be different you need to compile them separately.
javac -d ./build/classes ./src/cplx/*.java
javac -classpath ./lib/junit.jar -d ./build/test ./test/cplx/*.java

Although it's nice to start off using javac to grasp what is going on at a lower level and understand the language and tools before you begin using more advanced stuff, I think some Ant build script or an IDE like Eclipse or NetBeans would serve you better. At least if you just want a result rather than understanding all the details. You can always learn more about those later. Anyway, the official documentation should tell you what you need to know: http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

You have to specify one more thing in -classpath option..
it should be:
javac -classpath ./build/classes/:lib/junit-4.8.2.jar -d ./test/classes test/cplx/*.java
That's information where your Complex.class file is.

Related

Compile complex Java program in Ubuntu terminal

I have exported my Eclipse Java project in Ubuntu, with all the .java and .class files in place.
The project is composed as follows:
- src
- package1
- file1.java
...
- fileN.java
...
- packageM
- fileM.java
...
- fileN.java
To run the program, I use the following command:
java -Djava.library.path="/path/to/opencv/lib" -cp lib/*:src package.to.main.class.MainClass
Now, I have changed just a line in a class (which is not the MainClass), and I would like to recompile and run everything. However, when trying with javac in the following way:
javac path/to/main/class/MainClass.java
I obtain millions of errors since:
libraries are not found
other Java files are not linked
Some examples:
src/it/polimi/tweetcrawlingpipeline/pipeline/TweetCrawlingPipeline.java:7: error: package org.opencv.core does not exist
import org.opencv.core.Core;
^
symbol: class SVMSample
location: class TweetCrawlingPipeline
src/it/polimi/tweetcrawlingpipeline/pipeline/TweetCrawlingPipeline.java:158: error: cannot find symbol
public GenericClassifier<SVMSample> getTextClassifier() {
^
How can I fix these problems?
Thanks.
This might not be the answer you want, but it rapidly gets to be painful to compile at the command line with just javac. Unless you put your command and classpath into a shell script, it can get fiddly.
If at all possible, I would recommend using something like ant, gradle, or even maven. Well maven can be overkill, but ant is a reasonable start.

StandardAnalyzer does not exist

Based on this tutorial I tried to create my own basic Lucene search application. You can also use the given example directly, it has no bearing on my question.
So my file B.java begins with:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
And although I have supplied the classpaths in javac,
javac -cp ~/lucene-4.10.2/core/lucene-core-4.10.2.jar
:~/lucene-4.10.2/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
I get errors like this:
B.java:1: error: package org.apache.lucene.analysis.standard does not exist
I use the plural because this also occurs for other packages where the .class is inside a given JAR (checked using unzip -l). I assume if I can find the solution for this, it should apply for the other cases as well... what am I missing out here?
OK, I seem to have found the answer.
For some reason, when including the ~ which expands to my $HOME directory, this makes javac unable to find the correct path:
javac -cp ~/lucene-4.10.2/core/lucene-core-4.10.2.jar
:~/lucene-4.10.2/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
I made a soft link to the directory instead:
$ ln -s ~/lucene-4.10.2/ lucenepath
Then using the symlink, it worked:
javac -cp lucenepath/core/lucene-core-4.10.2.jar
:lucenepath/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
Not sure why this is the case.

How to include Java classes when compiling from Command Line?

I've usually used an IDE for my Java programs but I've recently switched to the Command Line.
I created several classes that are in the same directory as my "main" program that I wish to compile. However, the command 'javac -cp test.jar ColorTester.java' yields:
ColorTester.java:24: cannot find symbol
symbol : class IntToColor
location: class ColorTester
IntToColor colorFunc = new ModThreeToColor(testColor,
^
My classes IntToColor and ModThreeToColor have both been compiled into class files, but it's still not making a difference.
The IDE had always taken care of this for me, so as you can imagine I'm quite confused.
Thank you.
Like this:
java -classpath <here> Program
Replacing <here> with the list of classes and/or jars and/or packages that make up your classpath, separated by the : character on Unix (or the ; character in Windows). There's plenty of documentation online, for example read this link.
include the current directory in the command line class path.
javac -cp test.jar:. ColorTester.java

Compiling java program with cp switch - unexpected behavior

I'm not able to understand certain behavior while using -cp switch with javac. I have two java files in the directory C:\A\B\C> of a Windows 7 machine. The files are Extend.java and TestExtend.java; both belong to the package 'package com.gonni.profile'. I'm getting the following error:
C:\A\B>javac -d . -cp C\Extend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
C:\A\B>javac -d . -cp 39#$%$fe#%#$%FF#$%GWE C\Extend.java
C:\A\B>javac -d . -cp C\TestExtend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
C:\A\B>javac -d . -cp 3458$^$%$%BF#W%V#$ C\TestExtend.java
C\TestExtend.java:6: cannot find symbol
symbol : class Extend
location: class com.gonni.profile.TestExtend
Extend exObj = new Extend();
^
C\TestExtend.java:6: cannot find symbol
symbol : class Extend
location: class com.gonni.profile.TestExtend
Extend exObj = new Extend();
^
2 errors
C:\A\B>javac -d . -cp . C\TestExtend.java
C:\A\B>
Extend.java is :
package com.gonni.profile;
class Extend {
class Inner {
}
}
TestExtend.java is :
package com.gonni.profile;
class TestExtend {
public static void main(String[] args) {
Extend exObj = new Extend();
}
}
I am sorry to say it but I do not understand what do you want to do: to compile your program or to make javac to fail?
Path C\TestExtend.java seems wrong. Do you probably mean C:\TestExtend.java?
What is 39#$%$fe#%#$%FF#$%GWE? Do you understand what does -cp mean?
Your classes belong to package com.gonni.profile. It means that they must be under directory com/gonni/profile starting from your source root.
You do not have to supply option -d .. This is a default.
As far as I understand you have several (2 ?) classes without any external dependencies. This means that you do not have to use -cp (that means CLASSPATH) at all.
What to do?
Create directory where your project is. Let's say C:\myproj.
To simplify things for the beginning create directory structure according to your packages. For exampplee if your package is com.gonni.profile you should create directory C:\myproj\com\gonni\profile.
Put your class(es) there.
Open commend prompt and go to C:\proj
Now run command javac com/gonni/profile/*.java
Good luck.
Your source files in this case should be under directory C:\A\B\C\com\gonni\profile - not directy in C:\A\B\C. Option -cp specifies path(s) to look up other compiled classes - not the source files.
Use -sourcepath instead if you want to specify location of source tree:
javac -sourcepath C C/com/gonni/profile/TestExtend.java
Javac requires to list ALL files it must compile in the command line. You cannot just list one and except it to autodiscover others. As a result, large, real world projects are very difficult to build that way. Also, fix a couple of small errors others have already pointed out.
Hence learn Eclipse, NetBeans or the like for IDE-based development or learn Maven, Ant, Make or the like if you want to become a command line master. It is uncommon just to call javac directly at these times.

On Java class path, clarification needed

In the following scenario:
APP_HOME=/Users/me/Documents/workspace/Mimer/bin
javac -cp $APP_HOME/lib/*.jar:: BCClient.java
Assuming $APP_HOME/lib contains all the jars needed
What would cause the following:
BCClient.java:35: package com.thoughtworks.xstream does not exist
..
It looks like files libraries needed by the .java are not found, except that when i do the following code fails with the same error
javac -cp "$APP_HOME/lib/xstream-1.2.1.jar;$APP_HOME/lib/xpp3_min-1.1.3.4.O.jar" BCClient.java
This should work with a compiler Java 6+. But if you execute this command from a shell that perform wildcard expansion, then you need to put the wildcards in quotes. More details can be found here.
/Library/Java/Home/bin/javac -cp "$APP_HOME/lib/*.jar:." BCClient.java

Categories

Resources