Missing JavaFX application class - java

I have java code like this:
package mypackage;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApp extends Application{
public static void main(String args[]){
launch(args);
}
public void start(Stage primaryStage){
primaryStage.show();
}
}
and I've compiled it at ~/myjava/src/mypackage/MyApp.class .
then, when I'm running from
~$ java -cp myjava/src mypackage/MyApp
why getting error like:
Missing JavaFX application class mypackage/MyApp
I'm using JDK 8.

That is because you are calling your application with a directory path instead of the fully qualified classname. The fully qualified classname is composed from the package name and the class name. In your case this is mypackage.MyApp.
Assuming that your compiled class resides in the same folder as the source .java file, call it this way:
java -cp myjava/src mypackage.MyApp

After installing Java 15 and JavaFX 13 and some other changes I too was getting "Missing JavaFX application class mypackage/MyApp" and in my case the package name and application were correct. I was using some JDK15 preview functions and I needed the JVM option --enable-preview. In desperation I started to change things, as this used to work. When I removed the --enable-preview option I received error messages relating to the fact I was using preview functions and did not specify it. This told me the loader was in fact finding my application. The error message "Missing JavaFX application class mypackage/MyApp" was incorrect in this case. The problem was I needed to include some user utility classes that MyApp was dependent upon. Once I included the dependent classes in the classpath all was well.

Related

Java program from command line fails. But Jar works fine [duplicate]

I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.
I have two simple classes,
package One;
import One.Inner.MyFrame;
public class test
{
public static void main(String args[])
{
MyFrame f= new MyFrame();
}
}
And the other class is,
package One.Inner;
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setPreferredSize(new Dimension(400,560));
setVisible(true);
}
}
I am at base folder "basic" in Windows cmd. I compile using
basic> javac *.java -d .
A folder and subfolder is created.
cd One
basic\One> java test
This generates a big set of errors. Many answers directed to specify the full path which didn't work.
My classes are in One so specifying One using -cp didn't work either.
You'd run it as:
java One.Test
... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name.
Oh, and package names in Java should be lower-case, so it should be one and one.inner, not One and One.Inner. Just a convention, but one which pretty much everyone follows.
If the directory is:
basic\One
Run java from the base directory of the package:
basic>java One.test or basic>One.test <optional arguments>
(ideally the package would be lowercase and the class upper case):
basic>java one.Test
If you get 'does not exist' messages, then the java command cannot find classes you referenced in your class. You can point to them with the -cp option ('.' means 'here', and you can add as many places as you like divided by ';' on Windows and ':' on Linux).
basic>java -cp . one.Test
or
basic>java -cp .;..\..\someJar.jar;c:\someDirectory\classesDirectory one.Test
The following line of Haralan Dobrev code solves the problem.
java -cp ../ one.Test
while creating a class with a package if you want to run it from cmd you must created a directory with same name of package put the .class in it and then you can easily run it for example you created a class with name "one" and this class in the package with name pack ,you must run these commands
1 javac one.java
after compilation created a directory with the name pack ,after that run this command
2 java pack.one
Note:
all this must be done in the current working directory and the name "one" i chose it here as file name and main class name
we all know the first name used in the first command is file name and second one is main class name
This is because if you are declaring package in your java file, then JAVA compiler believe you are having same folder architecture in your system.
In your case Java compiler looking for One as a package and then test.class., or to be very specific just look inside your .class file you can see what path it looking for. Please have a look for below Image (I my case I use Hello and Tester)
As you can see path in image is Hello/Tester(my case example), so architecture should be like Hello->Tester.
And if you are not having same architecture and want to create same while compiling, then use javacp command.

Can not find or load main class - classloader issue

I know this question has been asked before, but I’ve read through most of them and still can’t figure out my problem.
I’ve compiled the below code into "HelloWorld.class" located in the directory shown below.
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
But I cannot get the .class file to run using the Java command on Window Command Prompt:
I believe my PATH and CLASSPATH are set correctly (as shown below). But I still can’t find the class. Any ideas why?
The name of the class is not HelloWorld. It goes like:
java helloworld.HelloWorld
and you have to call that from the directory above helloworld.
In other words: the package name is part of the class name. When you invoke java; you have to provide that full name; and the classpath needs to point to the directory that contains that package.

Controlling the loading of Java class in CLASSPATH

I am trying to apply patch using classpath approach, I mean I am adding modified class files as jar file, and while classes are getting loaded new version of classes are loaded. Therefore application is patched without changing original jar file.
The following classpath definition works fine;
java -cp patch/patch.jar;bin/ com.test.PatchClasspath
but when order of lib classes are changed it does not work.(as usual)
java -cp bin/;patch/patch.jar com.test.PatchClasspath
I would like to know is there a JVM parameter which indicates the lib loading order?
EDITED:
I amd modifying Util->print() method to verify patch is applied.
package com.test;
public class PatchClasspath {
public static void main(String[] args) {
Util util = new Util();
util.print();
}
}
package com.test;
public class Util {
public void print(){
System.out.println("Version-1");
}
}
Thanks.
There is no such parameter indicate the lib loading order in JVM (I believe), however, the java -classpath option itself will determine the class loading order base on the paths you put.
JDK document explain this:
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html
Specification order
The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only if it doesn't find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.
Try -verbose:class, this will show you all the loaded classes, in which order they were loaded and from which jar they were loaded.
To control the order of classes loaded, you can modify the order of jars in your classpath, using the java -cp. I do not think there is a way howto control the order of classes being loaded from a specific jar.
To verify that the patch is applied, a simple/dummy solution is to add a static field with the System.out.println(""). For Example:
static {
System.out.println("[DBG] : My Patch v1.0 is loaded.");
}

Java classpath confusion

I wrote and tested a small Java program using Eclipse. I'm now trying to deploy it on a Windows 7 box and Java cannot find the class. I copied the class file to C:\dxtester\classes. I'm trying to run it from the dxtester directory with: C:\dxtester>java -classpath classes;. dxtester
This produces this exception which I think I understand. Java examined the class file and is prompting me to provide the fully qualified name.
Exception in thread "main" java.lang.NoClassDefFoundError: dxtester (wrong name:
dxtester/dxtester)
If I use the FQN I get
C:\dxtester>java -classpath classes;. dxtester.dxtester
Error: Could not find or load main class dxtester.dxtester
The application is a simple test driver where everything is done in main().
package dxtester;
public class dxtester {
public static void main(String[] args) {
This seemed like an extremely simple thing to do but I'm completely baffled. What am I missing?
Your current directory is dxtester;
in this directory you have dxtester.class (I presume);
your classpath is the current directory.
This setup is wrong: your classpath must be the base directory such that package names correspond to its subdirectories. In your case you should cd to C:\ and repeat the command; ideally, however, you will have your package structure in a dedicated directory instead of the root.
I should also mention that class names should be in CamelCase.

Exception in thread "main" java.lang.NoClassDefFoundError

package pack;
public class sample{
public static void main(String input[])
{
NumberFormat numberFormat = new DecimalFormat("#,##0.00##");
System.out.println(numberFormat.format(44533125.00));
}
}
the code is working fine in the current dir.. (c:/myprogram/).
after that i copy the sample.class file and paste it in other dir(d:/myprogram).
i got error while running, like
Exception in thread "main" java.lang.NoClassDefFoundError: sample (wrong name: pack/sample)
In java .class file can run anywhere right? but why i am not able to run?
You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run
java pack.sample
(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)
If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:
javac -d classes Sample.java
or
javac -d classes src/pack/Sample.java
will (in both cases) create
classes/pack/Sample.class
You could then run
java -cp classes pack.Sample
IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it.
The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.
If you are not using a single java/class file you can also remove the package statement.

Categories