Java - NoClassDefFoundError on running - java

Today I tried to compile my sources through the command prompt:
PS ...\JavaDev\Prog> javac -classpath <libs> -d . -sourcepath src src/com/negi/prog/Prog.java
They compiled successfully.
But when I try to run it, it produces an error:
PS ...\JavaDev\Prog> java -classpath com.negi.prog.Prog
Exception in thread "main" java.lang.NoClassDefFoundError: com/negi/prog/Prog
Caused by: java.lang.ClassNotFoundException: com.negi.prog.Prog
How can I fix that?

The classes in your -classpath have to be separated by :
PS ...\JavaDev\Prog> java -classpath "<libs>:com.negi.prog.Prog"
To complete the answer, the different operating systems have different classpath separators. You can check the separator by retrieving the value of the java.class.path property.

By default . (current path) is included in class path, but if you specify -classpath or -cp, then that is overridden. Include . in your classpath:
java -classpath <libs>:. com.negi.prog.Prog

You need to ensure the current directory is on the classpath when running i.e.
PS ...\JavaDev\Prog> java -classpath <libs>:. com.negi.prog.Prog

Related

Passing JAR files as CMD arguments that are required as dependencies

I have a program running in Intellij Idea. It has the following dependencies added as a jar file
I want to run it using the command line and want it to detect the jar files automatically as dependencies. Is there any way to achieve that without using Gradle or Maven? Or I could pass the dependencies as command-line arguments?
I have tried this command but it throws an error:
javac -classpath lib/*.jar -sourcepath src/*
Error:
error: invalid flag: lib/okhttp-3.4.1.jar
Using java -cp says this:
java -cp lib/* -sourcepath src/*
error
Error: Could not find or load main class lib.okhttp-3.4.1.jar
Caused by: java.lang.ClassNotFoundException: lib.okhttp-3.4.1.jar
Used the suggested command and it says:
java -cp lib/gson-2.10.jar:lib/okhttp-3.4.1.jar:lib/okio-3.2.0.jar:lib/slf4j-api-2.0.6.jar src/Main.java

Linux equivalent of including the classpath during compilation

I'm following a guide that only includes compilation instructions on windows. How would one run this build.bat file on Linux?
The batch file looks like this:
#echo off
#echo Compiling...
javac -classpath ..\..\lib\OneWireAPI.jar;%classpath% -d . .\src\*.java
And when I run the javac command on Linux, it fails:
javac -classpath ../../lib/OneWireAPI.jar;%classpath% -d . ./src/ReadTemp.java
The output is:
javac: no source files
What is the correct way to do this?
On Linux, you have to use : (colon) in place of ; (semicolon) as the path separator in Java options.
Also, if you have a classpath variable, in most common Linux shells it is referenced by $classpath rather than by %classpath%
javac -classpath ../../lib/OneWireAPI.jar:$classpath -d . ./src/ReadTemp.java
You have two items that did not get translated correctly from Windows CMD to Unix:
Path separator ; should be :.
Environment variables should be changed from %classpath% to $CLASSPATH format. Note that pretty much everything is case-sensitive in Linux, including environment variable names, and the Java path is traditionally all-caps.
Try
javac -classpath ../../lib/OneWireAPI.jar:$CLASSPATH -d . ./src/ReadTemp.java

"error:cannot acces com.hello.LibC","Class file for com.hello.LibC not Found"

I am new to Android.I use this link to run the NDK Project.
I follow all these steps from the given link.However during compilation in the command prompt it shows error like:
$ javah com.hello.LibC
error: cannot access com.hello.LibC
class file for com.hello.LibC not found
javadoc: error - Class com.hello.LibC not found.
Error: No classes were specified on the command line. Try -help.
please help me.
Thanks in Advance.
you need to specify the classpath using -classpath PATH option when running javah inside bin directory of your project:
$ javah -classpath classes/ com.hello.LibC
also you can specify the output directory using option -d PATH
$ javah -d ../jni -classpath classes/ com.hello.LibC

specifying log4j in classpath

I believe this is how I can compile and run a file that uses external library. I'm using Windows.
top level directory
|
|-log4-1.2.17.jar
|-MyApp.java
|-com
|-foo
|-Bar.java
Compiling
javac -cp log4j-1.2.17.jar;. com\foo\Bar.java
javac -cp log4j-1.2.17.jar;"com\foo";. MyApp.java
Executing
java -cp log4j-1.2.17.jar;"com\foo";. MyApp
Compiling itself failed.
simple batch script, for compiling all your project
set COMPILED_CLASSES=.\
set TEMP_FILE=temp
dir .\*.java /s /B > %TEMP_FILE%
javac -classpath log4j-1.2.17.jar;%COMPILED_CLASSES% -d %COMPILED_CLASSES% #%TEMP_FILE%
rm %TEMP_FILE%
add it to top level dir and run
EDIT
step by step
javac ./com/foo/Bar.java -classpath log4j-1.2.17.jar
next
javac ./MyApp.java -classpath log4j-1.2.17.jar;./
run
java -classpath log4j-1.2.17.jar;./ MyApp
Include current directory in java classpath
java -cp log4j-1.2.17.jar;. MyApp
Why do you have to include current directory:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
Y need to include the local directory. If you want to do it in the current directory it would be something like:
javac -cp .;log4j-1.2.17.jar Bar

javac no source files found

I have the .java file on the current working directory but javac reports:
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
I'm working on ubuntu.
From your comment above, it looks like you tried:
javac -cp .;lib.jar a.java on your Ubuntu system. The CLASSPATH separator is : on Unix systems and ; on Windows.
Ubuntu considered the command up to the ;, java -cp . and thus gave the message.
javac -cp .:lib.jar a.java should compile fine.
For anyone who is using powersehll on windows use CLASSPATH separator : instead of ;
I tried a similar thing and found that you need to mention the absolute path when you are using the
-cp and -d option with javac like this
javac -cp 'ur location of jars & files'; -d 'location to add your classes to' 'absolute path of file'
eg:
javac -cp C:\home\lib\mywork; -d c:\home\classes c:\home\files*.java
for javac, there are options and arguments
arg: it takes argument as path of source file
options: we require for basic compilation
-sourcepath: the path of dependent source files
-d: directory path of output classes
javac -sourcepath './src' -d './bin' -verbose './src/App.java'

Categories

Resources