r/javahelp • u/[deleted] • Jul 17 '23
Could we just get ONE working build/package system?
I haven't done Java in a long time. I'm a polyglot though and will pick up any language that makes sense for my task at hand and right now I need a Windows desktop application with an embedded web browser so I've packaged up the CEF browser into a window, built some nice Swing based chrome for it, and then I need to access some local hardware from my web app so I've added the appropriate bridge handlers. Cool.
But I have to say, coming from places like Rails and PHP Composer, Gradle and Maven are just garbage. I've got both maven based and gradle based versions of this app working, downloading the JCEF package and a couple others and they build and run. However, I need to talk to some local hardware and there are Java wrapped native libraries to make that work I got from the vendor. About two dozen jars each with a companion .dll native library on Windows. I'm struggling with getting those jars recognized. They contain some class definitions I need but the editor (using Intellij) doesn't seem to resolve them. The documentation isn't any too good either.
My goal, ultimately, is to build a Windows 10 application. Any tips would be great, thanks.
EDIT: gradlefile:
plugins {
id 'java'}
group = 'app' version = '1.0-SNAPSHOT'
repositories { flatDir { dirs 'libs' } mavenCentral() maven { url 'https://jitpack.io' } }
dependencies { testImplementation platform('org.junit:junit-bom:5.9.1') testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'me.friwi:jcefmaven:110.0.25'
implementation 'com.sun.net.httpserver:http:20070405'
fileTree(dir: 'libs', includes: ['*.jar'])}
test { useJUnitPlatform() }
Maven:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>app</groupId>
<artifactId>App</artifactId>
<dependencies>
<dependency>
<groupId>me.friwi</groupId>
<artifactId>jcefmaven</artifactId>
<version>110.0.25</version>
</dependency>
<dependency>
<groupId>com.sun.net.httpserver</groupId>
<artifactId>http</artifactId>
<version>20070405</version>
</dependency>
</dependencies>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties></project>
3
u/mambo5king Jul 18 '23
You'll want to install the jars into your local repository and then reference them as dependencies. https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
-1
Jul 18 '23
Why do I need a local repository vs a plain directory? Ultimately I want to package this thing into a Windows Application.
3
u/OffbeatDrizzle Jul 18 '23 edited Jul 18 '23
The local maven repository is just a database of ALL your dependencies (even ones not related to this project if you have multiple projects). There are various ways of packaging java applications and you configure what you want the end product to be in the pom - e.g. just a class file, jar, uber jar, etc.
If you're using a newer version of java then you can just use jpackage.
There's a LOT of plugging various things into one another in basically unlimited combinations in the java world, hence the need for everything to be configurable.
edit: Also the http server you are using in your pom looks to be from 2007... ???
1
u/ThisHaintsu Jul 18 '23 edited Jul 18 '23
If you are just want to get things started and just want to add a custom jar to your Maven project, systemPath should work for you: https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project#22300875
Also please try mvn verify in the project directory first as IntelliJ in standard configuration does not use Gradle or Maven (it uses its own build system).
1
Jul 18 '23
You actually pick a build system on project creation. I've managed to create both a maven and a gradle version for this project but I haven't had any luck with getting the jars recognized. I have over two dozen of the things BTW. That's a lot of typing in maven.
1
u/ThisHaintsu Jul 18 '23
Yeah, but that is not what IntelliJ uses internally — it just translates the Gradle or Maven file (like
pom.xml) automatically on the fly. That can cause friction between the state on the Maven side and on the IntelliJ side.As for your problem: If these JARs are all in a folder — why don't write a simple bash file that generates the import statements for you?
2
u/OffbeatDrizzle Jul 18 '23
Project structure -> libraries (or artefacts) -> add the jar(s).
Or just add the jar via the -cp flag when you're running the java command if you want to test a packaged application out.
Maven and gradle both work fine. If the dependencies you need are not on maven central (or installed to a local maven repository) then no, it's never going to find them and you'll have to install it manually as above in the IDE or by installing locally like this. If you want to use a dependency that already exists in maven central, however, then it literally is a case of adding 2-3 lines to your pom.xml.
The tools aren't the problem here...
2
1
u/AutoModerator Jul 17 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
Limiting your involvement with Reddit, or
Temporarily refraining from using Reddit
Cancelling your subscription of Reddit Premium
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/NitronHX Jul 18 '23
Regarding your gradle code - I am sure you can include local files (/libs) as dependencies : but fileTree() is definetly not one of them. fileTree just fetches asll files from there. To register them as dependencies you need to wrap it in 'implementation' or whatever your scope is.
As for maven there you use SYSTEM as the "scope" to include local files.
But I am sure that a google: gradle use local jar as dependency should yield correct results.
1
u/wildjokers Jul 20 '23
but fileTree() is definetly not one of them
Incorrect. See example 8 here: https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:file_dependencies
What OP is missing is a configuration name in front of it like
implementation0
u/NitronHX Jul 20 '23
Yes as I said! fileTree is a method on the Project levwl and can be used anywhere,
implementationone of the methods that adds a dependency (and is only usable in the dependency block) and can accept a fileTree as argument.Nevertheless my point was that the docs (at least for gradle) are quite clear about it and I am confused how OP didn't find it
0
u/NitronHX Jul 18 '23
2 seconds of googleing later: https://www.baeldung.com/gradle-dependencies-local-jar
here you go
if you don't want to follow links:
implementation fileTree(dir: 'libs', include: '*.jar')here you go
1
u/wildjokers Jul 20 '23 edited Jul 20 '23
First, format your build.gradle and pom.xml correctly. (put 4 spaces before every line) so it is easier for us to read, if it is easy for us to read it is easier for us to help you (see rule 13 in sidebar).
fileTree(dir: 'libs', includes: ['*.jar'])
You need to add a configuration in front of that, for example:
implementation fileTree(dir: 'libs', includes: ['*.jar'])Also, if you use fileTree then you don't need this: flatDir { dirs 'libs' }
Java ecosystem already has two build tools that both work just fine, Gradle and Maven. The build tools aren't the problem.
0
Jul 17 '23
Attached the maven and gradle files above, not at all sure how to get my /lib/*.jar files included.
2
u/istarian Jul 18 '23
Included how? It shouldn't be that hard to just have them on the classpath at runtime?
Also, proper Reddit formatting can be a bear. It would probably be easier to host the maven, gradle files elsewhere and provide link. E.g. use a pastebin
1
Jul 18 '23
That's my question - included how?
Not clear on what that looks like and all the "examples" I google are snippets devoid of context - often then don't even say what file the snippet goes in.
2
u/istarian Jul 18 '23 edited Jul 18 '23
Generally you would put any other needed dependencies (typically another JAR file) in the same folder as the primary JAR file itself or in a subfolder of that one.
Then you would add a Classpath header/directive to the primary JAR's manifest file indicating where to find those things.
example directory structure
dist/ project.jar dependency1.jar lib/ dependency2.jarI'm not sure how to do it anything other than Ant, but maybe this webpage is useful?
https://mkyong.com/maven/how-to-create-a-manifest-file-with-maven/
It looks like for maven you just need to specify the desired plugin to use and provide it the needed data. The file they're showing the contents of is your pom.xml file.
If you're not clear on what's going on there is a short "Reference" section at the bottom of the articles with some links to the maven documentation and one to a previous article on building jars with maven.
https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.htmlThe above tutorials on Oracle's website are only current for Java 8 at the latest and therefore outdated with respect to later versions but might be a useful refresher on the general way that JARs work and the basics of manifest files.
P.S.
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
1
u/istarian Jul 18 '23
Personally I found Gradle to be nearly incomprehensible the last time I tried to figure out how to use it so...
•
u/AutoModerator Jul 17 '23
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.