I am using IntelliJ and I would like to package a library I found into a jar for my project. Said library was found online and only contains java files without a main class. Before someone flags this for being a duplicate, it is not. All other questions are with projects that have a main class. I tried following https://stackoverflow.com/a/45303637/10002144, but it does not work because I get to a point where it says "META-INF/MANIFEST.MF file not found in 'libname.jar'. I also tried making a gradle project and building it, but it simply creates a jar that is only 297 bytes long, so obviously that doesn't work.
What can I do in my case?
What are you using as a package resolver?
For maven, gradle, sbt projects you can simply run the package instruction (i.e. mvn package) and it will jar your project regardless of a main class.
Related
I have a springboot application, it runs just fine in intellij. I'm trying to create an executable jar to run the application on other machines. I followed this
tutorialhttps://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/
down to every step, and managed to build my jar. but when i try to run the jar with
java -jar nameofjar.java
I get a Could not find or load main class error.
I unzipped the jar file with java c and the classpath correctly points to the main class, so im really lost as to what i have to fix here to get java to detect the main class.
I've tried a bunch of solutions from here:
Error: Could not find or load main class in intelliJ IDE
such as deleting .idea or Rebuild Project or doing mvn clean package but nothing seems to work.
Using java -c to open the jar file and verify that the main classpath is indeed correct
Main class and file structure:
This run config works perfectly fine in intellij
here is my manifest.mf:
Manifest-Version: 1.0
Main-Class: com.owl.PosApi.OwlPosApiApplication
If you build jar artifact using IntelliJ IDEA, it may be incomplete or manifests from the other included dependencies can override your custom one with the main class defined.
Related answers:
https://stackoverflow.com/a/42200519/104891
https://youtrack.jetbrains.com/issue/IDEA-116209#comment=27-656589
https://stackoverflow.com/a/45169655/104891
https://stackoverflow.com/a/42660624/104891
The proper solution would be to build the artifact using Maven. It may work out of the box with mvn clean package or you may need to modify pom.xml to build the fat jar with all the dependencies per this document.
I am trying to build a web application based on the project library CitySDK, but i don't know how to import it properly into my own project.
I have created a Maven Web Application and added the CitySDK library as a Dependency, and then Selected the Project, right-clicked and Selected Build with Dependencies. No errors appeared, and in the image below you can see it appeared as a dependency.
However, whenever i try to use some of the classes specific to it, in my own Web Application(called TourismApplication's TestClass.java), the compiler displays an error, that the package is unknown. Could anyone suggest ideas as to what i have missed? I have followed a few Maven tutorials, but without any effects.
The dependencies are as follows:
Ok I did some digging and I think I figured out your problem. Their POM file is incorrect based on the structure of project.
First off, I'll paraphrase what I think the steps you took to get it built in your project were, to ensure I followed the same steps to get it working. These are the steps I took:
Cloned/downloaded the sources from the link you placed in the OP
Built the project into a jar file by running the command they said to use: mvn clean package assembly:single
Installed the artifact in your local Maven repo using mvn install
Added a dependency in your project POM
I tried the same thing you did, using the library in a test source file, to no avail. I looked at the .jar file that was built using their instructions and didn't find any .class files archived into it... it was essentially empty.
Turns out, their src folder structure follows Maven standards, but their POM file indicates the sources are down a different chain of directories. If you examine the build log closely, you see "[INFO] No sources to compile"
The POM.xml file they provide specifies the source directory as
<sourceDirectory>src/citysdk/tourism/client</sourceDirectory>
However, the actual files are at
src\main\java\citysdk\tourism\client
After changing the line in the POM file from the above to (similarly for tests):
<sourceDirectory>src/main/java/citysdk/tourism/client</sourceDirectory>
<testSourceDirectory>src/test/java/citysdk/tourism/client/tests</testSourceDirectory>
rebuilt, and installed, it worked when I tried to use it in my project. Hope this helps.
FYI, I used IntelliJ as my IDE, but it should work the same with Eclipse.
I have to write a java application which I'm putting together using eclipse and it relies on open source code. This application needs to be self-contained, meaning that I'm supposed to create a jar file that has no external dependencies.
I can use the open source code when I reference the jar files in the project's build path, but the idea is to have the actual source code as part of the eclipse project, side-by-side with my code.
The source code can be found here: http://hc.apache.org/, but when I import an existing file system into my project I can't quite get things to work. The packages end up with the wrong names, breaking references, and I can't do anything. Notice that the folder containing the source code has this structure:
httpcomponents-client-4.2.3\
src\
httpmime\
httpclient-osgi
httpclient-contrib
httpclient-cache
httpclient-benchmark
httpclient
fluent-hc
each of those subfolders has src/main/java/org/apache subfolders.
Can someone please explain how to do this? Am I supposed to import everything one java file at a time?
Use a tool like OneJar, FatJar, JarJar, etc. to create a single-jar application.
As Charlie mentioned, the Maven Shade plugin is another choice, particularly if you're already using Maven. If you're not, consider it or another transitive dependency management tool.
Some tool should be used, IMO, and it's more important the more dependencies you have.
Alternatively you could use a jar class loader and include the jar file in your artifact.
I would most definitely not include the source of dependencies in your own project.
in my project there are classes containing #Transactional(rollbackfor=Exception.class). When compiling and packaging my project, a jar file is created. However, looking at the #Transactional(roolbackfor=Exception.class) classes, I saw #Transactional(rollbackfor={null}). What is the reason of that _? And What is the solution ?
Thank you
I downloaded the project, but I don't see any problem. When I import the jar in a different project I still see the correct rollback for.
I issued mvn clean install on command line to generate the jar. I had a look at your committed jar. Yours looks similar to mine, no problem even with it. I even decompiled it and I can see the rollbackFor={Exception.class} in AzerAuthorityDaoImpl.
So I've been pigeon-holed into writing some Jython code. I've been using the latest version of Eclipse IDE with the PyDev plugin for development. Up until now, things have been moderately tolerable. I've gotten all my Python scripts working and I'm successfully including a couple of JAR files and the class directory of another Java project as external dependencies. Everything seems to run fine through the Eclipse IDE.
Now I need to package everything up and deploy it. From what I can gather, the best way to do this would be to package everything up in a JAR file. The Jython documentation suggests starting out with the jython.jar file and adding to it. OK. So I modify my main python module and start adding all my python source to the JAR.
It executes but of course can't find all the external dependencies.
How is one supposed to add the external JAR files so that they are correctly seen by the Jython interpreter? How is one supposed to manage more complex dependencies in a setup like this?
Is there a plugin for Eclipse or maybe something like Ant or Maven that can handle all of these steps for me with the push of a button?
I can't be the first person that has needed to deploy Jython code with complex dependencies can I?
I've made some headway on getting this all working so I thought I would put some notes here in case they help anyone else out. I'd still like to hear from others on their experiences trying to put together something like this.
It turns out that Eclipse as of 3.5 has a project export option for Java -> Runnable JAR File. If you use this option, you can point to a Java main class in the export wizard. You also have the option to have it repackage all the JARs that you are dependent on in your new JAR file. Make sure to check the box to save the export as an ANT build so that you can repeat the process quickly. NOTE that the first time you do this through the interface, it may fail, but it will still have created a JAR file.
Now here's where it gets strange. To track all the dependencies, I am still using a mostly incomplete Maven build in my project. I create the Maven .POM file. And I told Maven what my external JAR dependency was. I then told Maven to do a dependency update for me. It pulled everything into my Maven repository as expected.
Now when I do my ANT build, it appears that it is getting its list of JARs to include in the final build from Maven. I'm not really sure if it is supposed to work that way. I'm also not 100% sure that it is working. I guess I'll find out when I have to add another external JAR to the project.
Anyways, if you follow this question you'll see that you can take the latest builds of Jython and pull the org.python.util.JarRunner.java file out and use it in your own project. This is you Java.main class that you will need to point your ANT build at. From there, convert your main Python/Jython script to be the run script that was talked about in that question.
Next, build up another copy of the Jython JAR file in your Jython directory. This one should have the /Lib directory pulled into the JAR. Save that off and then point your Eclipse IDE Jave Build option for your PyDev project at that JAR as an external dependency. Your JarRunner will now work and execute the run.py file under Jython.
If all that works, you should then be able to rerun the ANT exported build from earlier and you will end up with a single JAR file that you can execute on the command line as:
java -jar {yourjar} args
And distribute to your customers without any additional dependencies.
If that all seems a little bit convoluted, it is. If anyone has a better way of setting this all up using Eclipse, please let me know.
Make your life easier and just use Maven and the mavenjython compile plugin.
See my answer of this question: Using Jython with Maven
You get full automation of the build and deploy process, and the result is a jar that includes jython and all other dependencies.