i'm trying run a project with this architecture...
And, when i press "Run" button the code gives me this error...
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x709a3ba3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x709a3ba3
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2135)
at ui.Main.start(Main.java:32)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application ui.Main
My code of my Main...
package ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.FIBAManager;
import java.io.IOException;
public class Main extends Application {
private static final String FOLDER = "fxml/";
private FIBAManager manager;
private MainGUIController MGC;
private EmergentGUIController EGC;
public Main() throws IOException {
manager = new FIBAManager();
EGC = new EmergentGUIController(manager);
MGC = new MainGUIController(manager, EGC);
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage window) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(FOLDER + "MainWindow.fxml"));
fxmlLoader.setController(MGC);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, null);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
window.setScene(scene);
window.setTitle("");
window.show();
}
}
And i have this run cofiguration:
--module-path "C:\Users\Giova\Downloads\javafx-sdk-17.0.0.1\lib" --add-modules=javafx.controls
I need help to solve this problem and run the project please
You are using fxml so you need to add the fxml module, otherwise it will not be visible for use:
--add-modules javafx.controls,javafx.fxml
See the openjfx.io documentation titled Run HelloWorld using JavaFX SDK.
Example execution command for Linux from that source (modified to add the javafx.fxml module):
java --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloFX
Alternately, you can define a module-info.java file which requires javafx.fxml and any other modules required for your application. See: Understanding Java 9 modules.
module <your module name> {
requires javafx.controls;
requires javafx.fxml;
opens <your package> to javafx.fxml;
exports <your package>;
}
Substituting your info for:
module <your module name> any name you want, but best to put it as the name of the primary package of your app, e.g. com.example.javafx.myapppackage. Follow Java 9 module naming conventions.
opens <your package> to javafx.fxml a package which uses fxml (opens is required to allow the fxml package to reflect on your code).
exports <your package> a package you want to make visible (via export) to users of your application's module code; e.g. the package which contains the class in your appliction with a main(args) method.
Or, you can ignore the module system using the hack provided in mipa's answer, as mipa does have a very good point about the additional complexity involved in using the module system. It is not a supported configuration by the JavaFX developers, but will likely work OK for your app.
Just append this line to the file containing your Main class.
class MainLauncher {public static void main(String[] args) {Main.main(args);}}
Then put all your dependencies on the classpath (also JavaFX) and in your command line remove everything that is related to modules. Then launch the main from the MainLauncher class and be happy. You'll see a little warning. Just ignore it. This module stuff is simply unnecessarily complicated.
There is a jdk created by Liberica that has JavaFX integrated into it so you can compile with javac and run with java. Make sure you download the "Full" JDK. This eliminates a lot of headaches.
Related
Since JDK 11, the java command can launch a java source code file, i.e. no need to first compile your java source code. Here is my java source code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class HelloJavaFxWorld extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Hello JavaFX World");
Pane root = new Pane(label);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I save this code as file HelloJavaFxWorld.java.
I open a command prompt window and enter the following command.
java HelloJavaFxWorld.java
This is (part of) the output.
HelloJavaFxWorld.java:1: error: package javafx.application does not exist
import javafx.application.Application;
How can I run a JavaFX source code file?
Since JDK 11 is modular, you need to add the JavaFX modules. Try the following.
java -p "path\to\javafx.graphics.jar;path\to\javafx.base.jar;path\to\javafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Replace path\to with the actual path to the JAR files. For example on my Windows 10 machine I have installed JDK 16.0.1 so I am using JavaFX 16 and have placed the [JavaFX] JAR files in this folder:
C:\Program Files\Java\javafx-sdk-16\lib
So my actual command for launching the JavaFX source code file is:
java -p "C:\Program Files\Java\javafx-sdk-16\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Note that instead of -p, you can use --module-path. Then the command becomes:
java --module-path "C:\Program Files\Java\javafx-sdk-16\lib" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Note that I enter that command from the folder containing the java source code file.
The above command may result in the following exception.
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: HelloJavaFxWorld
at javafx.graphics/javafx.application.Application.launch(Application.java:310)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
Caused by: java.lang.ClassNotFoundException: HelloJavaFxWorld
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:466)
at javafx.graphics/javafx.application.Application.launch(Application.java:298)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:415)
at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
This is because, when launching a java source code file, the source code is compiled and the compiled class is stored in memory, i.e. no .class file is created. Method launch(String...), in class javafx.application.Application, calls method forName, in class java.lang.Class in order to load the JavaFX application class. Since there is no HelloJavaFxWorld.class file, method forName throws ClassNotFoundException.
In order to fix that, simply change your java source code to call the other launch method. In other words, change method main to the following.
public static void main(String[] args) {
launch(HelloJavaFxWorld.class, args);
}
Now, when I enter the above java command, I get the following window.
I received this error in my testing attempt of my template in java on netbeans in Ubuntu 12.04. What do I need to do from here based on the error information below the code. What is it saying that I should do to fix my problem.
Here is my code
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class again {
public static void main(String[] args) {
Mat m=Highgui.imread("/root/image.png",Highgui.CV_LOAD_IMAGE_OLOR);
}
}
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J
at org.opencv.highgui.Highgui.imread_0(Native Method)
at org.opencv.highgui.Highgui.imread(Highgui.java:296)
at again.main(again.java:22)
Java Result: 1
UPDATE:I changed the code and it said it compiled successfully but I still can't get it to show the image in a window. I also have another class thats suppose to be "interconnected" to show the picture but it doesn't.
public class again {
public static void main(String[] args) {
}
Mat m=Highgui.imread("/root/image.png",Highgui.CV_LOAD_IMAGE_COLOR);
LoadImage loadImage= new LoadImage("/root/image.png",m);
}
Here's the other class
import java.awt.*;
import javax.swing.*;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LoadImage extends JFrame {
public static void main(String[] args) {
}
public LoadImage(String imgStr,Mat m)
{
Highgui.imwrite(imgStr,m);
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
// Inserts the image icon
ImageIcon image = new ImageIcon(imgStr);
frame.setSize(image.getIconWidth()+10,image.getIconHeight()+35);
// Draw the Image data into the BufferedImage
JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
frame.getContentPane().add(label1);
frame.validate();
frame.setVisible(true);}
}
Sorry I couldn't get it to fit right, but I can't find out what version of java I am using or even worse I might not have java installed
root#ubuntu:/# sudo java –version
Exception in thread "main" java.lang.NoClassDefFoundError: –version
Caused by: java.lang.ClassNotFoundException: –version
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: –version. Program will exit.
The easiest solution seems to be to use the package of OpenCV provided by PatternDiscussing and use a maven project.
As described on their GitHub project page:
https://github.com/patternconsulting/opencv
I added:
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-7</version>
</dependency>
to my pom.xml file and this code before the main.
static {
nu.pattern.OpenCV.loadShared();
}
For an Ant project, which are just called Java Projects in Netbeans:
Download the jar itself from:
http://central.maven.org/maven2/nu/pattern/opencv/2.4.9-7/opencv-2.4.9-7.jar
Right-click Libraries under your project in the projects window, choose Add Jar/Folder, select the downloaded jar.
You also need to extract all the files from it with:
jar -xf ~/Downloads/opencv-2.4.9-7.jar
Instead of using nu.pattern.OpenCV.loadShared(); just call System.load() on the correct version of the shared library for your platform.
eg.
static {
System.load("/home/shackle/nu/pattern/opencv/linux/x86_32/libopencv_java249.so");
}
With either of those changes it worked for me.
You will need a modern version of the JDK.
To get the latest netbeans and the JDK on any platform:
Use the "Netbeans with JDK8" here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
To setup ubuntu repositories to pull in JDK 8.
Follow the instructions here:
http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/
Once that is done you may need to add that JDK as a platform and update the project settings to use the later JDK.
To add a platform:
Goto Tools-> Java Platforms menu in Netbeans and click the "Add Platform" button.
To update the project settings:
Select your project, right-click for properties, select libraries and select a different platform in the drop-down box labelled Java Platform.
I am trying a very simple use of JavaFX using a simple set of lines of code which I got from another stackoverflow page (here). But, the problem is clearly not with that code but with something more fundamental in the build and run process.
Here is my code:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
.
.
.
Media medMsg = new Media("msg.mp3");
MediaPlayer medplMsg = new MediaPlayer(medMsg);
medplMsg.play();
At first I couldn't get this to compile at all. Eventually I figured out that I needed to put -classpath c:\Program Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.jar on my javac command line. (One obvious complex of questions here is: Why isn't it documented in any obvious place (1) that this is needed and (2) how exactly to figure out what the path to the JavaFX installation is?!)
But, when I run the code I get:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/scene/media/Media
at progtest.main(progtest.java:120)
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
What does this mean? It looks like it doesn't know, at runtime, how to find the class javafx.scene.media.Media. But, my %CLASSPATH% variable definitely has "c:\Program Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.jar" in it.
Any ideas? Thank you in advance!
This question somewhat duplicates compile javafx 2.0 manually.
This answer is specifically for JavaFX 2 versions before the release of Oracle Java 8. For Oracle JavaFX 8+, the JavaFX runtime is on the classpath, so you don't need to explicitly add it when compiling or JavaFX running applications.
Java includes JavaFX in jdk7u6 and above (for Windows and Linux) and jdk7u4 and above (for OSX).
Download and use jdk7u6+ and you won't need to specify the jfxrt.jar file in any classpath and all of your JavaFX related classpath issues should go away.
Here is a link to an early binary build of jdk7u6.
For JavaFX 2.1 on Windows you do need to include the jfxrt.jar lib in your classpath for compile (NetBeans will do this automatically if you use it's JavaFX platform settings) and (if you haven't packaged your app correctly using the javafxpackager or JavaFX ant tasks), also at runtime.
JavaFX 2.1 for Linux is a pre-release (in case you are using that). For the Linux pre-release you would only have to include jfxrt.jar in your classpath at both compile and runtime if the JavaFX runtime on Linux was not set up correctly.
Here is an example of a command line compilation and execution of a JavaFX program under windows.
Launch an editor:
C:\dev\test>notepad HelloWorld.java
Paste the following code and save it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setScene(new Scene(new Label("Hello World")));
stage.show();
}
}
Compile and run it JavaFX 2.2:
C:\dev\test>javac -cp "\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld.java
C:\dev\test>java -cp ".;\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld
For Oracle Java 8+, the explicit JavaFX classpath specifier is not required:
C:\dev\test>javac HelloWorld.java
C:\dev\test>java HelloWorld
Note that usually rather than just compiling the code and running it, you would also package the code with javafxpackager or the javafx ant tasks. These tasks will embed a launcher class into your packaged app which will detect the version and location of the JavaFX runtime so that you don't need to specify the jfxrt.jar location unless you want to override the default location for the platform.
For Java 7 update 21, Windows XP SP3, I used
javac -cp "\Program Files\Java\jre7\lib\jfxrt.jar" HelloWorld.java
and
java -cp ".;\Program Files\Java\jre7\lib\jfxrt.jar" HelloWorld
I can compile this JNA example code (from step 2 of https://github.com/twall/jna/#getting_started):
package com.sun.jna.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
...using javac -classpath .:jna.jar -g HelloWorld.java without error. (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.)
But when I run it using java -classpath .:jna.jar HelloWorld, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/sun/jna/examples/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
I get the exact same exception on Mac OS X and Linux.
How do I get this to run?
This example (as well as vast majority of java classes) uses packages:
package com.sun.jna.examples;
In order to compile / run it properly you need to run javac / java from the "root" folder (e.g. folder where "com" is located):
Let's say you have a folder called examples. You'd put both the jna.jar and the source code in it preserving folder structure:
/examples
jna.jar
/com
/sun
/jna
/examples
HelloWorld.java
You compile and run using:
javac -classpath .:jna.jar -g com/sun/jna/examples/HelloWorld.java
java -classpath .:jna.jar com.sun.jna.examples.HelloWorld
Note the path separators in the former case and dots in the latter.
Either just remove this line and recompile (which is fine in this case as you just try out some sample)
package com.sun.jna.examples;
or read up on what packages in java are and how they have to be handled (ChssPly76s Posts as a starter).
Better choose the second option as sooner or later (probably sooner) you will have to deal with packages anyway. So just take the time now to read up on it.
In Eclipse, under Java Build path > Order and export, select export jna.jar.
I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello;
public class Hello
{
public static void main(String[] args)
{
coucou();
}
public static void coucou()
{
System.out.println("Hello there");
}
}
In a separate project called Tool, I want to be able to call the method Hello.coucou() so I wrote something like this:
import hello.*;
public class Tool
{
public static void main(String[] args)
{
System.out.println("main program running");
Hello.coucou();
}
}
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong.
Any ideas?
Cheers
David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar:
$ java Tool -classpath .:./extern/Hello.jar:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
You need the Hello.jar on the classpath when you run as well as when you compile.
Actually the trick was in the order of the arguments in the command line:
Is the -cp (or -classpath) is set last, then it doesn't work
java Tool -cp .:extern/Hello.jar
It has to be first like:
java -cp .:extern/Hello.jar Tool
!!!
Java uses dynamic late binding, so putting the JAR in the classpath during compilation is only necessary to ensure that your code is using the classes from it correctly, but it does not actually embed them into your code as the linker would in C/C++. Thus, you need to set the classpath also when executing the code.
However, this:
$ javac Tool.java -classpath .:./extern/:
should not work either, since JARs need to be put into the classpath directly, not just the directory they live in:
$ javac Tool.java -classpath .:./extern/Hello.jar
Finally, you are placing your code in the default nameless package. This is OK for fooling around, but will cause problems in the long run (for one thing, you cannot import classes FROM the default package anywhere else).
When you run Java you must add the jar file too (adding the directory path only does not work).
See classpath information.
It should be something like this:
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
You need to include the Hello.jar file in the classpath when you launch it too.
java -cp xxx.jar hello where xxx is the jar you want to have in your classpath, if you want multiple jars then separate them using ;
karl