Compile complex Java program in Ubuntu terminal - java

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.

Related

Javac Compiles Program, but Can't Run with Java

I've never had issues with compiling with javac and running with java before, but here we are.
The program, MainApp.java, depends on a jar file and a few other programs, so I ran the following (which ran without issue):
javac -cp gson.jar *.java
However, when it comes time to run it, I can't seem to get it to work. I've tried:
java -cp gson.jar MainApp.java
but it complains that it can't find the other java files.
I then tried listing the file it said it was missing:
java -cp gson.jar RepositoryData.java MainApp.java
but it gave the same error.
I then tried listing all the dependencies:
java -cp gson.jar Comment.java Issue.java Owner.java Repository.java RepositoryData.java MainApp.java
but it complains that it can't find a main function in the Comment.java file - this leads me to think I'm going about this the wrong way, and I probably shouldn't be directly referencing dependencies in the java call.
I also know that the program does work, as I've compiled it using an editor without issue, but I'm required to do this via the command line for a class.
How should I be properly calling it?

Could not find or load main class Javac

Quick note, I went through every single other question through here and nothing seems to work.
So, here is my issue. All I want to do is create a windows batch script that will be used to execute my selenium project on Jenkins. Sounds simple right ? Its probably is, but I am missing something...
Here is my project https://github.com/Daviditooe/Nomad
First command I tried:
javac src/nomad/execute/Execute.java
Execute.java:9: error: package nomad.sites does not exist import nomad.sites.MmaShare;
It also couldnt find any of the jars, so I add all of them
Then I tried:
javac -cp Jars\* src\nomad\execute\Execute.java
This fixed the jars issue but the package not found still exists
So then I tried compiling every single package at the same time
javac -cp Jars\* src\nomad\execute\*.java src\nomad\actions\*.java src\nomad\baseactions\*.java src\nomad\browsers\Chrome.java src\nomad\directory\*.java src\nomad\scripts\*.java src\nomad\sites\*.java src\nomad\urltools\*.java
So, now its not crashing, then I tried compiling...
java src\nomad\execute\Execute
And its giving me Could not find or load main class
So That last thing I tried was compile all of them at the same time.
java src\nomad\actions\MmaShareActions src\nomad\baseactions\BaseActions src\nomad\browsers\Chrome src\nomad\directory\Directory src\nomad\execute\Execute src\nomad\scripts\Vpn src\nomad\sites\MmaShare src\nomad\urltools\UrlTools
Still no luck... anything thoughts would be appreciated.
Again: You must go into the src folder before you execute javac and java!
I can compile and execute it on my Linux Laptop:
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ javac -cp '../Jars/*' nomad/execute/*.java nomad/actions/*.java nomad/baseactions/*.java nomad/browsers/Chrome.java nomad/directory/*.java nomad/scripts/*.java nomad/sites/*.java nomad/urltools/*.java
Note: nomad/baseactions/BaseActions.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ java -cp '../Jars/*:.' nomad.execute.Execute
Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: /hdd/stefan/Downloads/Nomad-master/src/C:\Users\Davidito\Desktop\AutoLinks\Jars\chromedriver.exe
Of course my Linux machine does not have the chromedriver.exe installed.
Under Windows, you must use "\" instead of "/" and you must use ";" instead of ":". Note that you must add the current folder "." to the class path when you execute the program. Otherwise java would only search in the Jars folder.

Could not find or load main class Java Error Notepad++

I've implemented "Java Compile" and "Compile and Run" (the second as Java Compile and Run) as described in this answer: Java compile and run using notepad++ and nppexec.
Note: I'm using 1.8.0_20 rather than 1.7.0 as described in the answer.
However, when I try to run HelloWorld (as found here: http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm) to test the compiler, I get the following response, including a "Could not find or load main class" error:
NPP_EXEC: "Java Compile and Run"
CD: C:\Users\Bova\Documents
Current directory: C:\Users\Bova\Documents
"C:\Program Files (x86)\Java\jdk1.8.0_20\bin\java" -classpath "C:\Users\Bova\Documents" "HelloWorld"
Process started >>>
Error: Could not find or load main class HelloWorld
<<< Process finished. (Exit code 1)
================ READY ================
What do I need to change to avoid this error?
I had this problem in Notepad++ too. What I did to fix it was I went to the Plugins menu, clicked on NppExec, and selected Follow $(CURRENT_DIRECTORY). After that I can run programs just fine.
The very first line of HelloWorld.java reads:
package helloworld;
Java packages are mapped to directories on the filesystem, so the interpreter expects to find HelloWorld.class inside a helloworld directory. Move the .class file to a helloworld subdir and run it as:
> java helloworld.HelloWorld
from the parent directory (i.e. the directory which contains helloworld/).

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

javac compilation - code in multiple folders

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.

Categories

Resources