How do I add my jar to Java compilation path? - java

I'm very new to Java programming and I'm trying to add jar to the classpath like this:
javac -classpath ~/Downloads/algs4.jar. ThreeSum.java
but still I'm getting the exception like:
ThreeSum.java:38: error: cannot find symbol
StdOut.println(a[i] + " " + a[j] + " " + a[k]);
^
symbol: variable StdOut
location: class ThreeSum
ThreeSum.java:62: error: cannot find symbol
int[] a = In.readInts(args[0]);
^
symbol: variable In
location: class ThreeSum
ThreeSum.java:64: error: cannot find symbol
Stopwatch timer = new Stopwatch();
^
symbol: class Stopwatch
location: class ThreeSum
ThreeSum.java:64: error: cannot find symbol
Stopwatch timer = new Stopwatch();
^
symbol: class Stopwatch
location: class ThreeSum
ThreeSum.java:66: error: cannot find symbol
StdOut.println("elapsed time = " + timer.elapsedTime());
^
symbol: variable StdOut
location: class ThreeSum
ThreeSum.java:67: error: cannot find symbol
StdOut.println(cnt);
^
symbol: variable StdOut
location: class ThreeSum
6 errors
I'm trying the programs from here

Looks like you need to download this JAR as well: http://introcs.cs.princeton.edu/java/stdlib/
They don't have a package structure, so unpack everything in the same directory:
jar xvf stdlib.jar
jar xvf algs4.jar
Then compile it:
javac -classpath . ThreeSum.java

You probably want ~/Downloads/algs4.jar. for that classpath to be ~/Downloads/algs4.jar:. on Linux or ~/Downloads/algs4.jar;. on Windows`

Related

Java code worked in Windows but not in Linux (Fedora or Ubuntu)

So I have this small Java project for study purposes where I am learning how to call functions from other java files and reference them with import. Everything works as inteded in Windows and I'm happy with it, but I just tried to work in the same project using Fedora and Ubuntu, and seems like the imports and package references just don't work.
I get this trying to compile main java file:
$ javac Ejercicio01.java
Ejercicio01.java:3: error: package actividad05.introduceDatos does not exist
import static actividad05.introduceDatos.Pregunta.*;
^
Ejercicio01.java:4: error: package actividad05.operaciones does not exist
import static actividad05.operaciones.Valores.*;
^
Ejercicio01.java:5: error: package actividad05.operaciones.algebraicas does not exist
import static actividad05.operaciones.algebraicas.Operaciones.*;
^
Ejercicio01.java:6: error: package actividad05.operaciones.geometricas does not exist
import static actividad05.operaciones.geometricas.Operaciones.*;
^
Ejercicio01.java:13: error: cannot find symbol
opcion = pideEntero("\nElija una opcion:"
^
symbol: method pideEntero(String)
location: class Ejercicio01
Ejercicio01.java:25: error: cannot find symbol
muestraPi();
^
symbol: method muestraPi()
location: class Ejercicio01
Ejercicio01.java:28: error: cannot find symbol
muestraValorAleatorio();
^
symbol: method muestraValorAleatorio()
location: class Ejercicio01
Ejercicio01.java:31: error: cannot find symbol
double num = pideDouble("Introduzca un valor: ");
^
symbol: method pideDouble(String)
location: class Ejercicio01
Ejercicio01.java:32: error: cannot find symbol
muestraSeno(num);
^
symbol: method muestraSeno(double)
location: class Ejercicio01
Ejercicio01.java:35: error: cannot find symbol
num = pideDouble("Introduzca un valor: ");
^
symbol: method pideDouble(String)
location: class Ejercicio01
Ejercicio01.java:36: error: cannot find symbol
muestraCoseno(num);
^
symbol: method muestraCoseno(double)
location: class Ejercicio01
Ejercicio01.java:39: error: cannot find symbol
muestraRaizCuadrada();
^
symbol: method muestraRaizCuadrada()
location: class Ejercicio01
Ejercicio01.java:42: error: cannot find symbol
double base = pideDouble("Introduzca base: ");
^
symbol: method pideDouble(String)
location: class Ejercicio01
Ejercicio01.java:43: error: cannot find symbol
double exp = pideDouble("Introduzca exponente: ");
^
symbol: method pideDouble(String)
location: class Ejercicio01
Ejercicio01.java:44: error: cannot find symbol
calculaPotencia(base, exp);
^
symbol: method calculaPotencia(double,double)
location: class Ejercicio01
15 errors
My main Java file references the other functions like this:
package actividad05.main;
import static actividad05.introduceDatos.Pregunta.*;
import static actividad05.operaciones.Valores.*;
import static actividad05.operaciones.algebraicas.Operaciones.*;
import static actividad05.operaciones.geometricas.Operaciones.*;
public class Ejercicio01 {
Meaning that Ejercicio01.java is in the folder src/actividad05/main, and the functions imported are in src/actividad05/introduceDatos/Pregunta.java etc, etc.
My first thought is that this is probably a classpath problem. Check where your source code is (.java files) and where you compile them to (.class files). Then check how you are trying to run the application, is the location of all the .class files on the classpath? In this case it has little to do with the OS difference, but with compilation and execution commands and file locations.
A problem which sometimes occurs when running compiled code on another OS is that the JDK or JRE has a different vendor or version. Are you using the Oracle or OpenJDK? Which version? If the version is different, even compilation might run into trouble because some Java features are different.
But the above output points in the direction of the first issue.
EDIT: the problem that the package is not found could mean that you don't have the .java file under the directories matching the package names, or that you are not calling the compiler on the root package (the source directory).
Check out: https://www.webucator.com/how-to/how-compile-packages-java.cfm

Using the controlling interface with NetLogo on windows

Can someone render assistance with invoking net logo on Windows. This works on Linux fine, but taking the example code from (https://github.com/NetLogo/NetLogo/wiki/Controlling-API) I get the following. It initially appears to be something with the java CLASSPATH but that is solved with the -cp arg, why it cannot pick from the environemnt I do not understand, but then I cannot run it. Please help.
c:\Program Files\NetLogo 5.3.1\app>echo %CLASSPATH%
"C:\Program Files\NetLogo 5.3.1\app\NetLogo.jar;."
c:\Program Files\NetLogo 5.3.1\app>javac \Users\robert\Documents\example1.java
\Users\robert\Documents\example1.java:1: error: package org.nlogo.app does not e
xistimport org.nlogo.app.App;
^
\Users\robert\Documents\example1.java:4: error: cannot find symbol
App.main(argv);
^
symbol: variable App
location: class Example1
\Users\robert\Documents\example1.java:10: error: cannot find symbol
App.app().open(
^
symbol: variable App
\Users\robert\Documents\example1.java:17: error: cannot find symbol
App.app().command("set density 62");
^
symbol: variable App
location: class Example1
\Users\robert\Documents\example1.java:18: error: cannot find symbol
App.app().command("random-seed 0");
^
symbol: variable App
location: class Example1
\Users\robert\Documents\example1.java:19: error: cannot find symbol
App.app().command("setup");
^
symbol: variable App
location: class Example1
\Users\robert\Documents\example1.java:20: error: cannot find symbol
App.app().command("repeat 50 [ go ]");
^
symbol: variable App
location: class Example1
\Users\robert\Documents\example1.java:22: error: cannot find symbol
App.app().report("burned-trees"));
^
symbol: variable App
location: class Example1
8 errors
Finally passing the class path as an arg gets it to complile.
c:\Program Files\NetLogo 5.3.1\app>javac -cp %CLASSPATH% \Users\robert\Documents
\example1.java
But running produces more errors.
c:\Program Files\NetLogo 5.3.1\app>java \Users\robert\Documents\Example1.java
Fel: Kan inte hitta eller kan inte ladda huvudklassen \Users\robert\Documents\Ex
ample1.java

Trouble Compiling and Running Java console application OS X Terminal

Cannot compile or run the Hangman console application I created in Eclipse. It is in my ~/Documents folder on my mac in a package called hangman. It doesn't see the two classes Game and Prompter that I created. I've tried using -cp but I'm not sure I am doing it right. It doesn't the ways I've tried it. Any help?
HoltnetMacbookAir:hangman godmanliving$ javac -classpath . Hangman.java
Hangman.java:20: error: cannot find symbol
Game game = new Game(args[0]);
^
symbol: class Game
location: class Hangman
Hangman.java:20: error: cannot find symbol
Game game = new Game(args[0]);
^
symbol: class Game
location: class Hangman
Hangman.java:21: error: cannot find symbol
Prompter prompter = new Prompter(game);
^
symbol: class Prompter
location: class Hangman
Hangman.java:21: error: cannot find symbol
Prompter prompter = new Prompter(game);
^
symbol: class Prompter
location: class Hangman
4 errors
You need to specify all files that need to be compiled.
It looks like in your case it would be something like this:
javac -classpath . Hangman.java Game.java Prompter.java
References:
how to compile multiple java source files in command line
http://www.codejava.net/java-core/tools/using-javac-command

javac fails with symbol not found

Here is my folder structure:
- myGame/
- bin/
- src/
- common/
- logic/
- main/
- messageprotocol/
- test/
- util/
- Test.java
- Rectangle.java
- Geometry.java
I would like to compile Test.java which is in util package. However, I get symbol not found errors, I guess javac command is wrong. Also, you should know that Test.java has references to other classes within the same project (myGame).
Here is what I do:
cd myGame/
javac -d bin -classpath bin src/util/Test.java
Once I'm done with above, I'd do this and it would work (I hope):
cd myGame/bin
java Test
I have read a lot of resources and tried to apply but it didn't work for my folder structure. What am I doing wrong?
Edit: Errors I receive are:
myGame/src/util/Test.java:16: cannot find symbol
symbol : class Rectangle
location: class util.Test
private static final Rectangle[] rectangles = new Rectangle[someNumber];
^
myGame/src/util/Test.java:16: cannot find symbol
symbol : class Rectangle
location: class util.Test
private static final Rectangle[] rectangles = new Rectangle[someNumber];
^
myGame/src/util/Test.java:20: cannot find symbol
symbol : variable Geometry
location: class util.Test
System.out.println("Printing " + Geometry.NUMBER);
^
myGame/src/util/Test.java:24: cannot find symbol
symbol : variable Geometry
location: class util.Test
serverSocket = new Circle(Geometry.NUMBER);
^
myGame/src/util/Test.java:36: cannot find symbol
symbol : class Rectangle
location: class util.Test
rectangles[i] = new Rectangle(number, rectangles);
^
5 errors
Package structure for your Test.java is util.Test so you should do:
cd myGame/src
javac -d ../bin -classpath ../bin util/Test.java
and then try running it. hope this helps.

Declare external libraries in Java - Compiling Error

I can't compile my Program.java from the command line (only in Eclipse).
When I want to compile it with
javac Program.java
"cannot find symbol" errors occur at JUnit classes.
Eclipse has the JUnit classes in it's plugins, but to compile it on my own I would need to somehow compile my JUnit.jar with the program. How can I do that? It doesn't seem to work with
javac -cp absolutePathTo\JUnitJar Program.java
Or is this problem maybe caused because the JUnit classes are not implemented in my (nested) new Thread classes?
C:\Documents and Settings\xxxx\Desktop\eclipse\xxxx\Program\src\da
ta>javac -cp junit-4.10.jar Testworks.java
Program.java:81: package org.junit.runner does not exist
import org.junit.runner.Description;
^
Program.java:82: package org.junit.runner does not exist
import org.junit.runner.JUnitCore;
^
Program.java:83: package org.junit.runner does not exist
import org.junit.runner.Request;
^
Program.java:84: package org.junit.runner does not exist
import org.junit.runner.Result;
^
Program.java:85: package org.junit.runner.notification does not exist
import org.junit.runner.notification.Failure;
^
Program.java:86: package org.junit.runner.notification does not exist
import org.junit.runner.notification.RunListener;
^
Program.java:253: cannot find symbol
symbol : class JUnitCore
location: class data.Program
JUnitCore jCore; //-> Core Runner - has no pleaseStop()
^
Program.java:254: cannot find symbol
symbol : class RunListener
location: class data.Program
RunListener jRl;
^
Program.java:255: cannot find symbol
symbol : class Request
location: class data.Program
Request jRq;
^
Program.java:2167: cannot find symbol
symbol : class RunListener
location: class data.Program
class RlOne extends RunListener{
^
Program.java:2170: cannot find symbol
symbol : class Description
location: class data.Program.RlOne
public void testRunStarted(Description descRun)
^
Program.java:2179: cannot find symbol
symbol : class Description
location: class data.Program.RlOne
public void testStarted(Description descStart)
^
Program.java:2185: cannot find symbol
symbol : class Description
location: class data.Program.RlOne
public void testFinished(Description descFinish)
^
Program.java:2202: cannot find symbol
symbol : class Failure
location: class data.Program.RlOne
public void testFailure(Failure failure)
^
Program.java:2211: cannot find symbol
symbol : class Description
location: class data.Program.RlOne
public void testIgnored(Description descIgno)
^
Program.java:2221: cannot find symbol
symbol : class Result
location: class data.Program.RlOne
public void testRunFinished(Result result)
^
Program.java:2422: cannot find symbol
symbol : variable Request
location: class data.Program.ThirdThread
jRq = Request.aClass(cRun);
^
Program.java:2426: cannot find symbol
symbol : variable Request
location: class data.Program.ThirdThread
jRq = Request.method(cRun, comb_meth.getSelected
Item().toString());
^
Program.java:2584: cannot find symbol
symbol : class JUnitCore
location: class data.Program
jCore = new JUnitCore();
^
19 errors
The java command output when I want to start it by the Eclipse compiled Program.class:
C:\Documents and Settings\xxxx\Desktop\eclipse\xxxx\Program\bin\da
ta>java Program
Exception in thread "main" java.lang.NoClassDefFoundError: Program
Caused by: java.lang.ClassNotFoundException: Program
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Program. Program will exit.
While using javac command with external jar, you should use
javac -classpath path-to-external-jar/jarname1.jar;/path-to-external-jar2/jarname2.jar Program.java
same for java command:
java -classpath path-to-external-jar/jarname1.jar;/path-to-external-jar2/jarname2.jar Program
Now I got it to work.
I renamed junit-4.10.jar to junit.jar, moved it to another folder and somehow this solved the problem:
javac -classpath "C:\Documents and Settings\x\Desktop\eclipse\x\Program\extres\junit.jar" src\data\Program.java
BTW:
When I'm in C:\Documents and Settings\x\Desktop\eclipse\x\Program\ it also works with:
javac -classpath extres\junit.jar src\data\Program.java
You really need to learn more on the topic of the Java CLASSPATH. Your errors, both at compile and at run time, are due to you not providing the complete classpath.

Categories