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
Related
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...
I just created a .java file.
(my IDE is NetBeans, java version java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode))
I want to compile it and then execute it from windows CMD because of the command line arguments.
So i write on CMD: javac className.java and everything works as expected.
After that i write the command: java className and this error pops out on CMD:
Error: Could not find or load main class AlgorithmsProject
Now on docs.oracle.com it says clearly on Common Problems:
"A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename"
Obviously i write and the className only and it gives me the same ERROR.
On youTube some videos show that you have to write the following commands on CMD:
set path="C:\Program Files\Java\jdk1.8.0_101\bin";
set classpath="C:\Program Files\Java\jre1.8.0_101\lib\rt.jar";
and the same error still pops out.
Any suggestions?
I'm Java newbie, but I had the same problem. I don't know if it helps you but you can just try it because it helped me:
javac className.java (just like you did earlier)
go one directory up (back) and copy its path to clipboard (for example C:\Users\XYZ\Documents\Exercise1\src)
in cmd write cd and paste the copied path with right mouse button and then click "Paste" so in cmd you are now moved one directory up
(it looked this way im my example:
cd C:\Users\XYZ\Documents\Exercise1\src)
java packageName.className (where packageName is the name of the folder containing your java file and the name you gave to the package when creating new project). In my example java file was under the following path:
C:\Users\XYZ\Documents\Exercise1\src\Exercise1\className.java)
The problem I'm currently having is that I am trying to execute a java file using command prompt, I understand the PATH being set to the jdk's file. Though my java file contains libraries and has to import the libraries, how would I ' import the libraries ' when it runs?
Sample command :
javac ClassName.java 1 1 1
When it executes it errors on the imports so what should I do?
The .jar files are Java "libraries". What you need is something like:
> javac ClassName.java
> java -cp Library1.jar:Library2.jar ClassName.class
The first line (javac) compiles the Java code into a class file. The second line runs the compiled class. The '-cp' option sets the CLASSPATH (makes the code in the jar files available at runtime). Note: the exact syntax will depend on if you are using Mac OSX/Linux or Windows. Windows uses the ';' character to separate the jar file names.
Several issues here:
Are you compiling or executing? javac is the compiler. java is the runtime virtual machine.
ClassName.java is source. ClassName.class is the compiled bytecode run by java.
As a general rule, your environment should contain the variable JAVA_HOME, and that should point at the directory where the JDK resides on your computer.
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
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