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
Related
Following instructions at https://openjfx.io/openjfx-docs/#install-javafx, I compiled the sample HelloFX.java via:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls /Users/me/Documents/java/HelloFX.java
But now if I attempt to run that...
java --module-path $PATH_TO_FX --add-modules=javafx.controls /Users/me/Documents/java/HelloFX
... I get error:
Error: Could not find or load main class .Users.me.Documents.java.HelloFX
Caused by: java.lang.ClassNotFoundException: /Users/me/Documents/java/HelloFX
Yet the file reported as not found is there:
ls -l /Users/me/Documents/java/HelloFX.class
-rwxr--r-- 1 me staff 1336 Oct 30 16:01 /Users/murray/Documents/java/HelloFX.class
(I had already changed permissions to add u+x in case that was the issue, but apparently that was not the trouble.
What's wrong?
(Yes, $PATH_TO_FX does point to javafx-sdk-11/lib.)
This question was already answered in the openjfx-dev mailing list:
The "java" command expects a fully-qualified class name, not a file path as
its argument
For completion:
The javac command deals with filenames, which means you can compile a java file from any location:
javac [ options ] [ sourcefiles ]
However the java command deals with classes:
java [options] mainclass [args...]
where mainclass specifies the name of the class to be launched, not the filename or location.
Providing you have Java 11 installed (and JAVA_HOME is set at it), the JavaFX 11 SDK downloaded, and just following the getting started guide:
Download the HelloFX class to any location, i.e /Users/<user>/Downloads.
Open a terminal and cd to that location:
cd /Users/<user>/Downloads
Set the JavaFX path:
export PATH_TO_FX=/path/to/javafx-sdk-11/lib
Compile the class:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX.java
Check that HelloFX.class is created at the same folder level.
Run the class:
java --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX
It should run just fine.
Now, if you try to run the above command from a different location it won't work, because the HelloFX class is not available in the classpath.
So if you want to run this class from a different location you'll need to specify this classpath:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls \
/Users/<user>/Downloads/HelloFX.java
java --module-path $PATH_TO_FX --add-modules=javafx.controls \
--class-path /Users/<user>/Downloads HelloFX
#!/bin/bash
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk
CLASSPATH=/Users/sunny/CronTest/out/production/CronTest
$JAVA_HOME/Contents/Home/bin/java -cp $CLASSPATH ".:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" sample.Main
exit 0
Sqlite jar file is in /Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar
Compiled java class file is in /Users/sunny/CronTest/out/production/CronTest/sample/Main.class
And I've set the cron job to be schedule at every 1 minute. Java class is getting exucuted but I am getting java.lang.ClassNotFoundException: org.sqlite.JDBC
Same command which is in script is working in terminal.
My question is how can I add jar file properly to be executed from shell script.
Seems there is a space in between your $CLASS PATH and ".:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" and missing a colon(:).
Please try with following with export:
export CLASSPATH=.:/Users/sunny/CronTest/out/production/CronTest:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar
and call java command with -cp $CLASSPATH.
If it still not work, please try with -cp directly:
$JAVA_HOME/Contents/Home/bin/java -cp ".:/Users/sunny/CronTest/out/production/CronTest:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" sample.Main
I'm trying this on cmd:
javac -classpath .;C:\Users\Myname\Documents\Tomcat_Apache\apache-tomcat-8.0.20\lib\servlet-api.jar ;C:\Users\Myname\Documents\Tomcat_Apache\apache-tomcat-8.0.20\webapps\mybay\WEB-INF\lib\jsp-api.jar
I'm getting this :
javac: invalid flag:
C:\Users\Myname\Documents\Tomcat_Apache\apache-tomcat-8.0.20\webapps\mybay\WEB-INF\lib\jsp-api.jar
Usage: javac 'options' 'source files'
I need this in order to use extends HttpServlet in java.
Can you help?
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
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