Declare external libraries in Java - Compiling Error - java

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.

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

How to fix javac package does not exist?

I am creating in my code a Java file, which I need to convert to a class. My file contains the following:
import com.company.tpch.TpchApplication;
import com.company.tpch.TpchApplicationBuilder;
import com.speedment.runtime.core.ApplicationBuilder;
public class java {
public void x() {
TpchApplication app = new TpchApplicationBuilder()
.withPassword("root")
.withLogging(ApplicationBuilder.LogType.STREAM)
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
.withSkipCheckDatabaseConnectivity()
.build();
}
}
When I try to compile the file with
javac -d . C:\Users\s\Desktop\demo\src\main\java\java.java
I have these errors:
import com.company.tpch.TpchApplication;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:2: error: package com.company.tpch does not exist
import com.company.tpch.TpchApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:3: error: package com.speedment.runtime.core does not exist
import com.speedment.runtime.core.ApplicationBuilder;
^
C:\Users\s\Desktop\demo\src\main\java\java.java:7: error: cannot find symbol
TpchApplication app = new TpchApplicationBuilder()
^
symbol: class TpchApplication
location: class java
C:\Users\s\Desktop\demo\src\main\java\java.java:11: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.APPLICATION_BUILDER)
^
C:\Users\s\Desktop\demo\src\main\java\java.java:10: error: package ApplicationBuilder does not exist
.withLogging(ApplicationBuilder.LogType.STREAM)
^
symbol: class TpchApplicationBuilder
location: class java
How can I solve this?
You need to add all classes that are used to your command. Like this:
javac com/company/tpch/TpchApplication com/company/tpch/TpchApplicationBuilder java.java
Of course you have to change this according to your directory structure, since I don't know it.
Anyway I would recommend an IDE that compiles your whole project at once like IntelliJ/Eclipse and/or a dependency tool like Maven/Gradle.

getting started with JUnit

Hi I am starting to learn junit but i Have following issues
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
TestJunit.java:1: error: package org.junit does not exist
import org.junit.Test;
^
TestJunit.java:2: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
^
TestJunit.java:2: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
TestRunner.java:1: error: package org.junit.runner does not exist
import org.junit.runner.JUnitCore;
^
TestRunner.java:2: error: package org.junit.runner does not exist
import org.junit.runner.Result;
^
TestRunner.java:3: error: package org.junit.runner.notification does not
exist
import org.junit.runner.notification.Failure;
^
TestJunit.java:4: error: cannot find symbol
#Test
^
symbol: class Test
location: class TestJunit
TestJunit.java:7: error: cannot find symbol
assertEquals("Junit is working fine",str);
^
symbol: method assertEquals(String,String)
location: class TestJunit
TestRunner.java:6: error: cannot find symbol
Result result = JUnitCore.runClasses(TestJunit.class);
^
symbol: class Result
location: class TestRunner
TestRunner.java:6: error: cannot find symbol
Result result = JUnitCore.runClasses(TestJunit.class);
^
symbol: variable JUnitCore
location: class TestRunner
TestRunner.java:7: error: cannot find symbol
for (Failure failure : result.getFailures()) {
^
symbol: class Failure
location: class TestRunner
11 errors
Source of learning: https://www.tutorialspoint.com/junit/junit_environment_setup.htm
I have the path:
%JUNIT_HOME%\junit-4.12
added to Path.
and the system variable
JUNIT_HOME | C:\JUNIT\
It looks like you are missing classpath in the run command.
What is a classpath in java
In Windows
Set the environment variable CLASSPATH to
%CLASSPATH%;%JUNIT_HOME%\junit4.12.jar;.;
Linux
export CLASSPATH = $CLASSPATH:$JUNIT_HOME/junit4.12.jar:.
Mac
export CLASSPATH = $CLASSPATH:$JUNIT_HOME/junit4.12.jar:.

chromium build error-package org.chromium.mojom.payments does not exist

As the subject line says, I couldn’t find the sources for the mojom payment package while compiling chromium for android.
Here are the list of errors.
../chrome/android/java/src/org/chromium/chrome/browser/mojo/ChromeServiceRegistrar.java:11: error: package org.chromium.mojom.payments does not exist
import org.chromium.mojom.payments.PaymentRequest;
^
../chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java:9: error: package org.chromium.mojom.payments does not exist
import org.chromium.mojom.payments.ShippingAddress;
^
../chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java:57: error: cannot find symbol
public ShippingAddress toShippingAddress() {
^
symbol: class ShippingAddress
location: class AutofillAddress
../chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java:13: error: package org.chromium.mojom.payments does not exist

classpath issue in hadoop java

javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
appClientModule/grid/comp/tools/CleanTmp.java:2: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:3: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
appClientModule/grid/comp/tools/CleanTmp.java:4: package org.apache.commons.logging does not exist
import org.apache.commons.logging.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:5: package org.apache.commons.logging.impl does not exist
import org.apache.commons.logging.impl.Log4JLogger;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
appClientModule/grid/comp/tools/CleanTmp.java:9: cannot find symbol
symbol : class JobConf
location: class grid.comp.tools.CleanTmp
JobConf jconf;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
7 errors
tools $ javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
jabir:CompareHdfs jabir.ahmed$ javac -classpath "/installs/hadoop-0.20.2/*.jar;/installs/hadoop-0.20.2/lib/*.jar" appClientModule/grid/comp/tools/CleanTmp.java
appClientModule/grid/comp/tools/CleanTmp.java:2: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:3: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
appClientModule/grid/comp/tools/CleanTmp.java:4: package org.apache.commons.logging does not exist
import org.apache.commons.logging.*;
^
appClientModule/grid/comp/tools/CleanTmp.java:5: package org.apache.commons.logging.impl does not exist
import org.apache.commons.logging.impl.Log4JLogger;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
appClientModule/grid/comp/tools/CleanTmp.java:9: cannot find symbol
symbol : class JobConf
location: class grid.comp.tools.CleanTmp
JobConf jconf;
^
appClientModule/grid/comp/tools/CleanTmp.java:8: cannot find symbol
symbol : class Log4JLogger
location: class grid.comp.tools.CleanTmp
Log4JLogger lg=new Log4JLogger(this.getClass().getName());
^
7 errors
tools $ ls /installs/hadoop-0.20.2/lib/*.jar
/installs/hadoop-0.20.2/lib/ant-contrib-1.0b3.jar /installs/hadoop-0.20.2/lib/jasper-runtime-5.5.12.jar
/installs/hadoop-0.20.2/lib/aspectjrt-1.6.5.jar /installs/hadoop-0.20.2/lib/jets3t-0.6.1.jar
/installs/hadoop-0.20.2/lib/aspectjtools-1.6.5.jar /installs/hadoop-0.20.2/lib/jetty-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-cli-1.2.jar /installs/hadoop-0.20.2/lib/jetty-servlet-tester-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-codec-1.4.jar /installs/hadoop-0.20.2/lib/jetty-util-6.1.26.cloudera.1.jar
/installs/hadoop-0.20.2/lib/commons-daemon-1.0.1.jar /installs/hadoop-0.20.2/lib/jsch-0.1.42.jar
/installs/hadoop-0.20.2/lib/commons-el-1.0.jar /installs/hadoop-0.20.2/lib/junit-4.5.jar
/installs/hadoop-0.20.2/lib/commons-httpclient-3.1.jar /installs/hadoop-0.20.2/lib/kfs-0.2.2.jar
/installs/hadoop-0.20.2/lib/commons-logging-1.0.4.jar /installs/hadoop-0.20.2/lib/log4j-1.2.15.jar
/installs/hadoop-0.20.2/lib/commons-logging-api-1.0.4.jar /installs/hadoop-0.20.2/lib/mockito-all-1.8.2.jar
/installs/hadoop-0.20.2/lib/commons-net-1.4.1.jar /installs/hadoop-0.20.2/lib/oro-2.0.8.jar
/installs/hadoop-0.20.2/lib/core-3.1.1.jar /installs/hadoop-0.20.2/lib/servlet-api-2.5-20081211.jar
/installs/hadoop-0.20.2/lib/hadoop-fairscheduler-0.20.2-cdh3u2.jar /installs/hadoop-0.20.2/lib/servlet-api-2.5-6.1.14.jar
/installs/hadoop-0.20.2/lib/hsqldb-1.8.0.10.jar /installs/hadoop-0.20.2/lib/slf4j-api-1.4.3.jar
/installs/hadoop-0.20.2/lib/jackson-core-asl-1.5.2.jar /installs/hadoop-0.20.2/lib/slf4j-log4j12-1.4.3.jar
/installs/hadoop-0.20.2/lib/jackson-mapper-asl-1.5.2.jar /installs/hadoop-0.20.2/lib/xmlenc-0.52.jar
/installs/hadoop-0.20.2/lib/jasper-compiler-5.5.12.jar## Heading ##
So how do i set the class path
I tried setting via the env variable too
$ echo $CLASSPATH
/installs/hadoop/lib/.jar:/installs/hadoop/.jar:/installs/hadoop-0.20.2/.jar:/installs/hadoop-0.20.2/lib/.jar
it still fails
$ javac -version
javac 1.6.0_29
Go for a more clear classpath like:
javac -classpath $HADOOP_HOME/hadoop-core-0.20.204.0.jar:$HADOOP_HOME/lib/commons-cli-1.2.jar -d inception src/Inception.java
(in this case Inception is your java program. don't know why i mentioned that but it's better to be more explicit then not!)
Most of the time you are going to need the hadoop-core-(yourversion) and the commons-cli if you are using the Tool interface.
If you have set the CLASSPATH variable, then you don't need to specify the -classpath flag in the javac command as that will take the classpath value from the environment variable CLASSPATH
The value of classpath needs to point to the actual jar file rather than .jar
For example,
set CLASSPATH=/myapp/mylib.jar
Depending on the platform you are on, you can either use : or ; as the delimiter between different paths. For Windows, use ; and UNIX normally uses :
In UNIX:
set CLASSPATH=/myapp/myclasses1.jar:/myapp/myclasses2.jar
But in Windows you would need to use semicolon as the delimiter between the paths
set CLASSPATH=/myapp/myclasses1.jar;/myapp/myclasses2.jar
inquire's solution helped me. Here is the syntax I used on HDInsight version 2.1:
C:\apps\dist\java\bin\javac -classpath %HADOOP_HOME%\hadoop-core-1.2.0.1.3.0.1-0302.jar;%HADOOP_HOME%\lib\commons-cli-1.2.jar WordCount.java
Instead of specifying individual libraries, just add " -classpath yarn classpath ". That makes the command pretty easy and simple.

Categories