My top level debug class as specified in the debug configuration is MyDebugClass. Further down in the main, it attempts to launch an instance of class ProblemClass that is in the same package as MyDebugClass but it errors out on the constructor invocation line saying "Source not found". The project compiles fine and the .class file is indeed in the target directory, the same place as the .class for MyDebugClass.
I thought there may have been something odd with the classpath so that the same package where the debug main is not included so I made a test class in the same directory and tried to invoke an instance of it and that worked. So that is not the problem.
I also added the following code in MyDebugClass.main to try to debug the issue:
ClassLoader loader = ProblemClass.class.getClassLoader();
Class probCls = loader.loadClass("mypackage.ProblemClass");
//here below is where it errors out
probCls.newInstance();
What could possibly be causing this kind of class loading problem? There is a whole bunch of jars in the classpath so could it be that one of them contains this same class qualifier and the class loader is trying to pick it up from there and not the target directory as specified in the debug build path? How do I find from where the class loader is attempting to load a class, e.g. file system path looking for a .class file or some jar?
Look at the file in the Eclipse "Package Explorer" view. That may give you some help. Also, you say that the .class file is in the target directory, but what about the source .java file?
Other things to check are the Java Build Path of your project properties.
I hope this helps, though you didn't really give enough detail to be able to understand what's really going on.
Related
I create the simplest possible modular project in Eclipse 4.21.0 (see below), with two source folders and one package. Both main methods just print "Hello".
If I right-click on Main1 and Run as Java Application, it's all fine.
If I right-click on Main2 and do the same, I get:
Error: Could not find or load main class p1.Main2
Caused by: java.lang.ClassNotFoundException: p1.Main2
If I check the two run configurations this generated (Main1 and Main2), they are (superficially) identical, except for the name of the Main class. If I go into the Main1 launch configuration and change the main class to Main2, now it works. So Eclipse generated a valid launch config for the top source folder, and a broken one for the second source folder.
Why is this, and how can I generate valid launch configs for other source folders?
Details
I'm aware that this is not an issue for non-modular projects, but I'm interested in solving the case for modular projects.
This happens even when I delete/refresh the launch config before launching, and independently of the order I run Main1 and Main2.
This happens whether I have a single output folder or one per source folder.
This turns out to be a known bug reported on 2020-11-23 for version 4.17: Cannot launch class if module-info.java is in additional source directory.
Workaround
As indicated in the original bug report, attempting to modify the broken configuration does not solve the problem:
If I edit the launch configuration (even if I just change the name) I get the following error instead on launching: Error occurred during initialization of boot layer...
However, it is possible to create a valid configuration for the main source folder (where module-info.java is located), and modify that configuration to use a main class from the additional source folder, which will run fine.
I am new to java,i have setup eclipse java project.I have a set of classes in jars in a folder.I have also got the java files associated to those classes downloaded (i cant compile those java files,its just to read and debug at runtime).My issue is class files are packaged in a different way than the those what appears in java file.
eg:- ABC.java is structured as platform\src\main\java\com\subplatform\sql\ABC.java
But package declared in java file if i open is
com.subplatform.sql;
I think this is the reason the debug point is not getting honored.In the previous version's of this product
java files used to be structured as
src\com\subplatform\sql\ABC.java
Thus it used to stop at the debug point.
Thanks
Rename all the package information that contains package com.[restOfPackageInfo] to be package java.com.[restOfPackageInfo] and you should be all set. (Also, if there are any import statements, you'll need to add java in front of com there too.)
There's your problem:
Originally the file structure was "src\com\subplatform\sql\ABC.java"
But by creating a folder called java and placing the com folder inside, you're changing the path to be: "src\main\java\com\subplatform\sql\ABC.java"
So, essentially in the ABC file you see package com.subplatform.sql;. This is information that the compiler uses to compile the class. So you're compiler looks for a ABC.java in src with path com.subplatform.sql but there is no such folder.
Also, read up on packaging.
It sounds like your source attachment path is wrong and should point to platform\src\main\java\ instead of the project itself.
A debug point not being honored can mean two different things.
1) The debugger isn't stopping on line breakpoints. If the compiled class files don't have line and debug information, the JVM doesn't know when it's executing those lines and will not stop there. You should still be able to stop at method entries and when exceptions are thrown. Either way, your are always required to launch your application using Debug instead of Run.
2) The debugger is stopping, but the source is not found. For a JAR file, you should have the properties of the JAR's entry on the Java Build Path point to a location where the source files exactly match the layout of the jar so that com/novice/to/sql/MyClass.class has a com/novice/to/sql/MyClass.java relative to where ever the source attachment points. If the top level contents of the JAR file contains entries like "com", "org", and "net", that's what should be in the top level of the source directory or archive you're pointing to.
My code has gotten quite large and so I've decided to build a runnable JAR to see how it's done. I probably should've tried sooner because I'm getting 2 different errors related to the project structure, first is the "no main manifest attribute error" when trying to run the JAR from command prompt. Double-clicking the JAR does nothing (Win7). The second issue is related to the FXMLLoader explained lower down.
I followed the steps here to build the JAR, which involved moving all Maven files into the JAR directory. The compiled JAR gave me the manifest error, so I followed this which adds a Maven plugin in my pom.xml file. The error might be caused by a wrong naming convention with the line <mainClass>com.primary.Drag</mainClass> where primary is the package and Drag is my Drag.java file (class) which has the main method.
Inititally I was using the default package but read that this is not recommended for larger projects, so I put all my files into "primary". Here is my current hierarchy as shown in IntelliJ:
The problem is that ever since I created the "primary" package, I can no longer even compile the program via IntelliJ, let alone build a runnable JAR. This is due by the second error I mentioned, which is java.lang.IllegalStateException: Location is not set. on this line within primary/Drag.java:
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("firstlaunch.fxml")); It used to work with the default package, but not anymore.
I tried replacing firstlaunch.fxml with /primary/firstlaunch.fxml and /resources/firstlaunch.fxml (with and without moving resources into primary package) but no luck.
3 Related Questions:
Is my project structure incorrect?
How do I reference the fxml file from the primary package?
Is this what I should write in Maven's mainClass tags? <mainClass>com.primary.Drag</mainClass>
Is my project structure incorrect?
Answer:
Your package name should be like com.primary.******
How do I reference the fxml file from theprimary package?
Answer:
Always make sure that you are trying to load firstlaunch .xml from the class which is located in same package where that xml is kept. Means class which you wrote the loading code and xml file should be in same package
Is this what I should write in Maven's mainClass tags?com.primary.Drag
Answer:
If you package name corrected to com.primary , your main class Drag will correctly added by maven
I created an Aspectj project and added .aj files and java files. While compiling(Load-time) it shows the error
"Error: Could not find or load main class javaagent:path/aspectjweaver-1.8.2.jar"
I compiled it in eclipse by creating Load time configuration file.
Can any one tell me why
Very simple: Probably you should provide a real file system path instead of the place holder path. Also be sure to use -javaagent:... (note the dash character) instead of something like javaagent:....
I started using JDBC so I went to Classpath, changed it to classpath of OracleDriver and program compiled. There was classpath set earlier there which I did not bother to check.Now, the program can compile but I am not able to run it.This is the error message I am getting:
Could not find or load main class 'classname that I am trying to run'
Any idea what's wrong?
After you compile, there are generated .class files that get made from the .java files.
When you want to run, you need the generated .class files on the path, so find the directory where they live, and add that directory to the class path.
Note that if you define a class as
package com.corp.myproduct;
public class Sifter {
}
It will generate to a path of
<root>/com/corp/myproduct/Sifter.class
or on windows
<root>\com\corp\myproduct\Sifter.class
where root is the "top level directory" of the compiled output. You don't add <root>/com/corp/myproduct/ in such a case to the classpath, you just add <root>.