Java on Windows command line - NoClassDefFoundError again - java

This question has so many answers, yet nothing works for me. I try to run a JAR file which I created and which depends on org.json library. I have my WebCfgSigner.jar and json-20180130.jar in C:\bin directory. It all works fine on my computer development computer:
java -jar c:\bin\WebCfgSigner.jar some parameters
I need to run it on my server (Windows Server 2016), where I installed first Java 1.8.something, then 17.0.2... All I get on the server is:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
I know about CLASSPATH environment variable, so I set it to all the variants below in turn, and nothing changes:
CLASSPATH=c:\bin\
CLASSPATH=c:\bin\*
CLASSPATH=c:\bin\json-20180130.jar
Then I try to add -classpath or -cp to the command line, no difference.
java -classpath c:\bin\json-20180130.jar -jar c:\bin\WebCfgSigner.jar some parameters
java -classpath c:\bin\* -jar c:\bin\WebCfgSigner.jar some parameters
java -cp c:\bin\json-20180130.jar -jar c:\bin\WebCfgSigner.jar some parameters
What else could I do? The json-20180130.jar is what works fine on my dev computer, but again not on the server...

Related

java command line compilation

I have a simple, single file java program that relies on a single static jar. The java code and the jar reside in the same directory. For this one-off solution I don't want to bring in the weight of ant or maven, and just want to compile it directly.
On my dev box, the following compiles and runs my code fine:
javac -cp ".;dependency.jar" File.java
java -cp ".;dependency.jar" File
However, on my test box, the java command fails, and I get the following output:
Error: Could not find or load main class File
If I change my classpath arg to -cp "." I get the following output:
Exception in thread "main" java.lang.ClassNotFoundException: dependency
My dev box is 64-bit Windows/Cygwin and java version 1.7.0_55. My test box is 64-bit Linux and java version 1.7.0_45.
What is going wrong on my test box?
The classpath separator character is different on Linux (and on Unix) than it is on Windows. It's ; on Windows, but it's : on Linux (and Unix).
Try this on Linux:
javac -cp ".:dependency.jar" File.java
java -cp ".:dependency.jar" File

Java on the Mac

I've been programming Java on windows for ages and just moved to the Mac.
I'm running the following command which works on the PC but doesn't on a Mac what am I doing wrong.
java -classpath ./lib.patches/*:./lib.core/*:./lib.custom/* test.Test
This gives me a ClassNotFound but test.Test is in a one of the jars in the path.
Thanks for all help.
OK I've debugged further and when I run this command
java -cp . test.Test
I've worked it out!!
The script was copied from a PC and had the wrong returns to end the lines.
This had the bizarre outcome of creating a ClassNotFound.
from the command line it works but from a script it throws ClassNotFound - must be a Mac issue ...
Works fine for me. (Escaping to show it's not related to shell globbing, but it works either way.)
With lucene-core in directory 1 and lucene demo in directory 2, or both in a single directory:
...lucene/lucene-3.4.0/tmp $ echo $CLASSPATH
...lucene/lucene-3.4.0/tmp $ java -cp ./1/\*:./2/\* org.apache.lucene.demo.IndexFiles -docs .
Indexing to directory 'index'...
adding ./1/lucene-core-3.4.0.jar
adding ./2/lucene-demo-3.4.0.jar
adding ./index/_0.fdt
adding ./index/_0.fdx
adding ./index/write.lock
1605 total milliseconds
If you also need class files based off the current directory, you should explicitly add the . path to the classpath.
Verifying #Dave Newton's result, using
$ java -version
java version "1.6.0_26"
with ThermometerDemo, this command works:
$ java -cp /opt/jfreechart/*:build/classes chart.ThermometerDemo
try
java -cp './lib.patches/*:./lib.core/*:./lib.custom/*' test.Test
instead

Ubuntu System Start-up: What command to insert to run java app?

In Ubuntu 10.10, System/Preferences/Startup Applications, I am trying to add a .jar program. If the program sits in home/john/this-folder/app.jar, what would I put in the command line for it to run on start-up?
java -jar /home/john/this-folder/app.jar [optional arg if any]
java - the Java application launcher
Note that you need to include jar in classpath if you java app needs dependecies.
You do that by:
java -jar /yourApp.jar -cp /home/zzz/libs/

matlab deploytool to java package javac error

I'm trying to wrap a program of mine to work with java.
I tried a simple "hello world" first,
-hello world.m-
disp('hello world');
I used deploytool and selected java package.
when it reached this line:
Executing command: "javac -verbose -classpath "C:\Program Files\MATLAB\R2009b\toolbox\javabuilder\jar\javabuilder.jar" -d "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\classes" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworld.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\DeployTutorial2MCRFactory.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworldRemote.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\package-info.java""
I got this error:
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
btw: when I tried standalone application / c/c++ shared library it has been compiled successfully.
thanks in advance
Possibly the Java SDK is not installed or properly configured on your machine. Open a system terminal and execute the following two commands:
java -version
javac -version
If they both work you should proceed with the examples from the MATLAB help. If not install the Java SDK.
First you should install JAVA.
Then you must set the environment variable in "my computer"
Add a new variable named "JAVA_HOME" and set its value to your jdk path
like D:\Program\Java\jdk1.6.0_25
Then restart your matlab
and type
getenv JAVA_HOME
you should get
ans=
D:\Program\Java\jdk1.6.0_25

How do I run a java program from a different directory?

I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

Categories

Resources