Convert Maver Project to "normal" Java Application project in Netbeans - java

This is my problem: I'm new at Java, I started a Maven project because I needed to use a lib that requierd that. Then I no longer needed it but keep going with the Maven project. Now I'm trying to build it but the .jar does not run the project when double clicked.
Since I don't know Maven very well (I found this but didn't understand it), I thought that the easiest way was to convert the proyect into a "normal" Java App project. So I copied all files with classes from the Maven project folder to a new Java App project folder manually.
It opens fine, but I got this error when retrieving an ArrayList:
Exception in thread "pool-1-thread-5" java.lang.ClassCastException: java.util.concurrent.CopyOnWriteArrayList cannot be cast to java.util.ArrayList
Since this doesn't happen in the Maven project I assume that it happens due to my archaic method to copy the classes...
So, is there a way to convert my Maven Project into a normal Java Project? I have found a lot of topics about doing the opposite but anything useful about what I need (only this but it is not what I need).
Thanks!

You can try closing the project on NetBeans and importing a project from existing file system.
However I feel that is not the ideal way to do it, but it should allow you to run it as you require.
Disabling the Maven nature is also viable.
According to Java Docs, java.lang.ClassCastException is
thrown to indicate that the code has attempted to cast an Object to a subclass of which it is not an instance.
There might be a chance that you are getting that exception cause you are missing some library or class which implements the same interface or allows you to convert the object to a given type.

Related

Can't create runnable file from project with NetBeans if it uses JavaFX

I originally asked another question regarding the specific issue of not being able to create an .exe file, but after trying three different methods that gave me three different errors, I've come to the conclusion that the problem has something to do with the use of JavaFX itself.
Long story short, I wrote a small program on Apache NetBeans 11.3 that uses JavaFX for the GUI. It has a class named Main that extends Application and another one called Launcher that does not, and whose purpose is to call Main.main(args).
I have tried in vain to create a runnable file of some sort for this program using NetBeans.
Under the Packaging project properties, I ticked Compress JAR File, Build JAR after Compiling, and Copy Dependent Libraries. Under Deployment I ticked Enable Native Packaging Actions in Project Menu and Keep JavaFX artifacts on Compile Classpath if not present by default. These are the same settings I found in every tutorial, although the tutorials I found were for version 8 or older of NetBeans.
When I try the standard "Clean and build" option, attempting to run the resulting .jar from the command prompt returns the following error:
Graphics Device initialization failed for : d3d, sw
Error initializing QuantumRenderer: no suitable pipeline found
Along with a ton of other lines I can copy/paste here if it's deemed necessary. I've tried the solutions I found online, but none of them worked.
I tried creating a Maven project, but the error returned is no main manifest attribute. Fixing this error by adding the location of the Launcher class to the MANIFEST.MF file (Main-Class: orionindustries.passwordalgorithm.Launcher) results in a different error: Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application.
I installed Inno Setup to try to create an exe file, but I get yet another error: typedef class com.sun.javafx.tools.ant.FXJar cannot be found
using the classloader AntClassLoader[].
I tried enabling the Switch Project to JavaFX Deployment Model setting under Deployment in the Project properties, but it says the JDK doesn't support JavaFX and doesn't allow me to select any JDK when I try to solve it.
I've no idea what else to do. In the previous (now-deleted) question someone mentioned JDK 14 would come with a packaging tool, but that's not a real solution to my problem, it's just using another tool for something that NetBeans is supposed to be able to do (and even then, there's no guarantee that it'd work). Either I'm screwing up massively, or NetBeans has serious bugs that somehow went unreported.

Best way to fix/modify a 3rd party jar without the source code

My application makes use of a third party jar that I want to improve and that has bugs in it. I can see from using a decompiler where the bugs are and can easily fix them. I am unable to extend the class and overwrite the problem methods as they are private and final, and make use of private and final fields.
At the moment, I solve this using a decompiler (which isn't perfect) to re-create the class in a project with the same class name, package etc. My gradle build script then extracts the contents of the original jar to $buildir/main/java so that when gradle compiles my version of the class, it overwrites the broken one, resulting in a new jar that is bug free.
I think this method is dirty and unmaintainable as if a new version comes out, merging changes could be difficult. Also, Eclipse and IntelliJ won't build the project when both projects are in my workspace with a compile project dependency, even though it builds on the command line, as the IDE is unable to see the source for all the extracted jar, but only the class I redefine to fix.
What is the best way to cleanly solve this problem?

Frege can't find classes in referenced project or external jar

I think I'm making a simple mistake here, but I can't get Frege to find any classes outside of the local Eclipse project.
I have a working non-trivial Java project (that's not mine), that I do not want to modify. I want to have a new clean Frege enabled project that makes use of classes from the original project.
I tried marking the original project as a dependency of my Frege project, and I tried packaging the original project into a JAR, and listing the JAR as an external dependency of the Frege project. In both cases, a Java file in the Frege project can access the classes, but the Frege compiler says "class org.foo.bar.Class is not a known Java class". This seems like a bug, but I am not confident that I have not missed a simple configuration step.
I have not tried setting arguments in the project configuration as I wouldn't know what to set.
I did quickly discover that I can make a new Java file in the Frege project with a blank subclass of whatever class I need and use that in a Frege file. I have successfully compiled and run a simple program like this. The program just makes a new object, gets a field, and prints the correct value, so I believe my Frege is installed and working properly.
More info:
Eclipse Luna 4.4.0
Java 7
No Maven
Official eclipse-ferge plugin installed through Eclipse
It should be enough to have your library listed in the build path and under "Referenced Libraries". Your recent comment indicates that the compiler does indeed find the class in question.
However, when you have an open editor tab it will not take notice of changed dependencies. Also, especially in recent eclipse versions I have observed that resolved error markers are sometimes not cleaned up correctly.
Please close the editor tab that has the false errors shown, and reopen it.

How to make Jar files in intellij IDEA without Main class

In my application I have 3 public classes (with a lot of external dependencies)
that I want to convert to a Java library ( Jar ).
Problem is when I try to construct the Jar using the standard system they ask me for a main class, which my source does not have.
Am I doing something wrong here?
Can someone please guide me on how to build a library using IntelliJ IDEA without a main class and with external dependencies
I use maven in IntelliJ, but you could use ant or gradle. In all these cases, it is the default behaviour to not include a main.
BTW, having a main for a library is not a bad thing, you can use it to print out information about the library such as when it was built or where the developer using the library can get more information.

NoClassDefFoundError at Runtime on class inside .AAR in Android Studio

I've been researching for many hours about a possible solution to this but whatever I try it just fails. Here's the explained situation:
I have this library project on Android Studio which generates the app-debug.aar file.
Then I add this new module in the implementing project, following the wizard to import .JAR/.AARs I pick the .aar and update the dependency for :app in the Project Structure.
Once the project is cleaned (even using ./gradlew clean--Mac OS X) I can make references of this class and its methods in any of the activities.
Now at runtime, once built and installed on the real devices the app will crash throwing the aforementioned Runtime Exception.
Note: I'm importing the .aar because I would need to protect the code and it has resources so a .jar is out of the question. And if I import the library module (with source and all instead of an.aar) then the app has no issues at runtime.
I've tried everything and if anyone can throw some light on what this could be, it'll be highly appreciated.
The class extends ViewGroup and is instantiated at runtime in case that implies anything. The code was given to me so major changes might not be possible unless it's absolutely necessary.
Thanks beforehand to all!
Armando
For those, who are still looking for the solution, following two options worked for me to solve the exact same problem as OP has mentioned.
Include the (problematic) library dependency in the target module as well, i-e the in my case i was including the protobuf-lite as a dependency to my library module but getting noclassdefFound error when lib imported as .aar in app module. As a workaround, i added the protobuf-lite dependency to my app module too and it worked like charm.
Second option that worked for me that instead of adding the gradle dependency `compile 'com.google.protobuf:protobuf-lite:3.0.1', i downloaded the protobuf-jar from maven and added manually to the libs of my library module, and the problem got solved.
Don't know what's wrong with the gradle plugin, but hope it helps someone else looking for the same problem.
The NoClassDefFound error has actually happening to a backward-compatibility library being used by this class I mentioned on the question ("Class A"). No details were given other than Class A couldn't be found and later on found out that another class had a similar issue BUT was pointing at that compat lib with the same exception and I noticed that Class A was also calling its methods and implementing its callbacks. By bumping up the min version (to use the native API) I could overcome that issue. It was indeed a problem of including that backward-comp library in the .aar or so it seems. Should I understand this issue a lot better I'll update this "answer."

Categories