Cannot determine main class after importing - java

I'm using Eclipse on a Mac. I'm learning java - previously used python.
Originally created a new package and it worked fine, using public static void main(String[] args)
In a separate package, I added acm.jar to my build path. That works fine: import acm.program.* and I use public void run()
Now my original package foo gives me an error:
Exception in thread "main" acm.util.ErrorException: Cannot determine the main class.
at acm.program.Program.main(Program.java:1358)
acm.jar isn't in my build path for foo. Do I have to remove acm.jar from all my packages?

Related

Java main within Tomcat project in Eclipse

I have a running and working Tomcat Project and I want to add a normal Java project into it for better and faster testing. No pages to get fast database outputs and such.
In the WEB-INF/src folder I have added a package with contains my main class and includes some of the other controllers from the project.
EDIT: Moved sources to the /src (as suggested) folder but still the same error.
Right now it is a simple println in a main class. The problem is that eclipse tells me it cannot find the main clas or load it. (right click on the main class file -> runs as -> Java Application)
package mytomcatproj.maintest
import mytomcatproj.othercontroller
public class MyMainClass {
public static void main(String[] args) {
String overval="te";
String overres= othercontroller.getWord(overval);
System.out.println("Test" + overres);
}
}
How can I get the project to run?
Thank you for you help.

Cannot run class files compiled by eclipse in command line

I compiled classes using eclipse, but when I try to run them in the command line, it returns "Cannot find or load main class".Command line.
My class is:
package Chapter10;
public class Hfpd10
{
public static void main(String[] args)
{
System.out.println("it works");
}
}
I am running the command from within the C:\Users\John\Documents\Java\EclipseWorkspace\HeadFirstDesignPatterns\bin\Chapter10 folder, where the class files are. The command is:
java Hfpd10
It runs in eclipse and I used the exact same path.
Eclipse path
The only question I could find like this went unanswered. cannot run java file in command line that created by eclipse
Change to the parent directory and run the following command:
java Chapter10.Hfdp10
This required is because your class is in a package and you need to reference it with its full package name.

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.

Missing JavaFX application class

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.

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.

Categories