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
Related
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
I have an executable-jar that I can run with java -jar app.jar but the SDK is 326MB. This is a lot.
jlink can create a JRE, but I can't use jlink as I have a non-modular application.
Can you please tell me how to create a JRE?
jlink can be used to create a 'JRE'/runtime image for non-modular applications just fine. It just can't automatically derive the modules that should go in the runtime image in that case. They have to be specified manually instead.
For instance, if I have a simple app.jar:
$ java -jar app.jar
Hello World!
Then create a runtime image with jlink, with only the java.base module:
jlink --output runtime --add-modules java.base --strip-debug --no-header-files --no-man-pages
Then I can run the jar with the java executable in the runtime image:
$ ./runtime/bin/java -jar app.jar
Hello World!
And the runtime image is just ~35 MB (though this can vary per platform).
jdeps can be used to get an idea of which modules should be used to create the runtime image:
$ jdeps --print-module-deps add.jar
java.base
This will print a comma-separated list of modules that can be passed directly as an argument to the --add-modules option of jlink.
You can try to this as source:
https://javaalmanac.io/
where you can find links to download only JRE instead of JDK.
I am creating a sample application with 3 modules user, dept and account. In my user module, I have a main class and compile my modules with the following command:
javac -d target --module-source-path src $(find -name "*.java")
After compiling, execute following command for run:
java -p target -m com.user/com.user.info.Launcher
The output after running java modules are successful. But when trying to create runtime image using jlink the image created successfully but module executable script is not there. For create an image, I am using the following command:
jlink --module-path $JAVA_HOME/jmods:target --add-modules com.user --output my-app
In, runtime image, I have bin directory, but this directory contains only java and keynote script. I am expecting user main class script as well, for executing my application.
My Java version as below:
java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+165)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+165, mixed mode)
How can I resolve this problem?
jlink creates a runtime VM image in which it includes only the modules that are needed.
Since you specified --add-modules com.user the image will include the com.user module, and all of the modules it (directly or indirectly) depends on.
You can run your application by using the java binary in the bin folder of the generated image, and using the command:
java com.user.info.Launcher
You can also have jlink generate a launcher script using the --launcher <command>=<module>/<main> option. In your case you could do something like:
jlink --module-path $JAVA_HOME/jmods:target --add-modules com.user --output my-app --launcher launch=com.user/com.user.info.Launcher
And after that, you can just use launch from the bin directory to run the application.
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?
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