Jump to content

IntelliJ to Jar problem

BelgianNoise

So I am trying to export as a jar file.

When i build the jar. it outputs a 57kb jar file, which i assume is just empty.
What am i soing wrong? also, is this the right way to include txt files and stufd?

This is how my files are layed out
image.png.e6b3f7f7e082f56502a1978b076496e5.png
 

 

This is the artifact i want to build
image.png.fb219c792249690b9f87759aa9ef17f9.png

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

57kB is pretty normal - it's compiled code, wtihout the JVM or anything. The source directory of a project I'm working on is about 280kB (which is more than yours looks to be), and the produced JAR, which I know to be complete, is 128kB.

 

I believe IntelliJ should include the resources too, but the best way to check is to rename the produced JAR to .zip (because .jar is just a .zip with a specific file layout), and open it in a zip viewer to check that it has the right files.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, colonel_mortis said:

57kB is pretty normal - it's compiled code, wtihout the JVM or anything. The source directory of a project I'm working on is about 280kB (which is more than yours looks to be), and the produced JAR, which I know to be complete, is 128kB.

 

I believe IntelliJ should include the resources too, but the best way to check is to rename the produced JAR to .zip (because .jar is just a .zip with a specific file layout), and open it in a zip viewer to check that it has the right files.

this is very helpful.

I can run it, but nothing happens, it is visible in task manager as well.
Maybe it is running the wrong class ?

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, BelgianNoise said:

this is very helpful.

I can run it, but nothing happens, it is visible in task manager as well.
Maybe it is running the wrong class ?

if you're running

java -jar path\to\produced.jar

it should run your application.Main class.

 

If you're trying to run it by just opening the jar, I would recommend running it from command prompt instead, so you get any error messages, and can also see when it finishes. I'm not running Windows so I'm not 100% sure if there's anything weird you need to do for it to work, but I think the command above ought to.

 

If you get a java.lang.NoClassDefFoundError, that means that either you're depending on a library that hasn't been bundled into the jar (if it's for a class that you didn't write), or that the JAR is wrong (if it's for a class you did write).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, colonel_mortis said:

if you're running


java -jar path\to\produced.jar

it should run your application.Main class.

 

If you're trying to run it by just opening the jar, I would recommend running it from command prompt instead, so you get any error messages, and can also see when it finishes. I'm not running Windows so I'm not 100% sure if there's anything weird you need to do for it to work, but I think the command above ought to.

 

If you get a java.lang.NoClassDefFoundError, that means that either you're depending on a library that hasn't been bundled into the jar (if it's for a class that you didn't write), or that the JAR is wrong (if it's for a class you did write).

im not familiar with the windows cmd for navigation.
What libraby should i download to run the jar on linux?

 

Also, i changed it to a zip, and everything is present

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

My Properties file fails to load
 

this.input = new FileInputStream("domain/db/evaluation.properties");

This line is giving the error. Checking the structure and it should work no?

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, BelgianNoise said:

My Properties file fails to load
 


this.input = new FileInputStream("domain/db/evaluation.properties");

This line is giving the error. Checking the structure and it should work no?

The relative path won't work, because the current directory is wherever you ran the command from, not within the jar. Instead, you need to use Class.getResourceAsStream, which will find the file in your include dir and open it as an InputStream. That code would become

this.input = Main.class.getResourceAsStream("/domain/db/evaluation.properties")

You'll need to make sure that this.input has type InputStream rather than FileInputStream for it to work.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

42 minutes ago, colonel_mortis said:

The relative path won't work, because the current directory is wherever you ran the command from, not within the jar. Instead, you need to use Class.getResourceAsStream, which will find the file in your include dir and open it as an InputStream. That code would become


this.input = Main.class.getResourceAsStream("/domain/db/evaluation.properties")

You'll need to make sure that this.input has type InputStream rather than FileInputStream for it to work.

I found out about that. also my txt files were not in any sourcefolder.
How is it that we dont even learn in school about how to create a jar or how a jar is actually created by the system.

One last question:
Can write to txt files in my JAR-file. All i found on the internet were big NO's.
But our teacher wants to be able to run the program from a single jar, but i cant write in it....or can i?


tnx real much btw, u ave thaught me more then the whole semester of school did.

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

46 minutes ago, colonel_mortis said:

The relative path won't work, because the current directory is wherever you ran the command from, not within the jar. Instead, you need to use Class.getResourceAsStream, which will find the file in your include dir and open it as an InputStream. That code would become


this.input = Main.class.getResourceAsStream("/domain/db/evaluation.properties")

You'll need to make sure that this.input has type InputStream rather than FileInputStream for it to work.

also I used 'getClass.getClassLoader.getResourceAsStream("...");"
Is that also good code ?

I am here to learn, please correct me if im wrong or you see me putting bs on your screen!

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, BelgianNoise said:

I found out about that. also my txt files were not in any sourcefolder.
How is it that we dont even learn in school about how to create a jar or how a jar is actually created by the system.

Partly because the way to make a JAR varies depending on what tool you use - I tend to use Gradle, which makes building a JAR really simple ("./gradlew jar"), while other people will use other build tools, their IDE (like you are) or the Java command line.

None of them are "best" - Gradle makes sense for larger projects, but adds a lot of bulk for a small school project - so there's not much point in teaching just one.

They could have told you how to access the files in the JAR, but often it can be beneficial to learn things like that for yourself, because it helps develop debugging skills.

5 minutes ago, BelgianNoise said:

One last question:
Can write to txt files in my JAR-file. All i found on the internet were big NO's.
But our teacher wants to be able to run the program from a single jar, but i cant write in it....or can i?


tnx real much btw, u ave thaught me more then the whole semester of school did.

I've not used IntelliJ's JAR builder a huge amount, but on my project I was able to get the resources to be included by editing the artifact and clicking +, "Directory content", then selecting my resources directory.

3 minutes ago, BelgianNoise said:

also I used 'getClass.getClassLoader.getResourceAsStream("...");"
Is that also good code ?

Yup, that's fine. The only thing with that is that you are getting the class at runtime (using the getClass method) rather than at compile time (using Main.class), but for that it doesn't matter.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×