Java - Read files outside a jar file - java

I'm in troubles.
I worked on a project on a wiktionary dump (that is a reaaaally huge file). Requirements asked that the dump file had to be passed as argument from command line. For making it easier I worked on NetBeans, I set all the parameters, put the dump file into the 'src' folder and everything went fine.
Requirements asked that the project had to be delivered as a jar file. I'm quite a newbie with jars, so I just copied netbeans src folder, compiled the code (from shell), removed the dump file -as it wasn't needed to be into the jar - and just did "compress as jar file".
Now comes the problem: When I run it from shell I get java.lang.reflect.InvocationTargetException caused by a java.net.MalformedURLException . I think the problem is that it looks for the dump only inside the jar, but actually the dump will be outside.. How can I fix it?
Don't know if it matters.. I'm working on ubuntu
Thanks everyone for your help!

Related

How to properly make jar archive to run Java program on different systems?

I have this, perhaps, easy problem, but I don't know how to handle this.
I have my Java program and it works pretty well when I call it via terminal (Java command).
The program uses 4 text files from the hard disk which can't be added as resources to the project.
How to do this right so I could build jar file only with main class and files from hard disk (first one is a config file and it has paths to other files so the program knows where they are)?
I'm using IntelliJ IDEA 14.1.4 on Arch Linux.
I did it based on this blog, but it's not working without txt files in src folder.
Also "jar cvf" command builds jar file, but it's not working outside my computer (for example on windows or OSX).
Can anyone help me?
I prefer step by step instruction so I would understand what is going on in here.
I recommend to build your application with Maven and create a Maven Assembly which contains your JAR file as well as the config.txt file.

Java3d - Jar file contains error java.lang.NoClassDefFoundError javax/media/j3d/Canvas3D

I am currently having a problem running a jar file created by eclipse.
Within eclipse I can run my project using the run function and the project runs exactly as expected.
The same applies to compiling and running the code from command line using javac Menu.java to compile and java Menu to run.
Both of these run correctly and do not return the error I receive when running the jar file.
The jar file was created in eclipse by exporting the project. But when i run the jar file the application refuses to response when opening any JFrames containing Java3d. Upon debugging the jar file i receive the error
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/media/j3d/Canvas3D
This normally would lead me to believe that my CLASSPATH is incorrect but since the project runs from command line this doesn't seem likely.
Any help or pointers would be very helpful.
Thanks.
Found a solution which works.
Instead of simply creating a jar file, you can created a runnable Jar file.
When asked under library handling in the export process, selecting 'package required libraries into generated JAR' solves this issue and allows it to run on all computers, even ones without Java3D installed.

Running a java project in terminal with multiple .java files (multiple attempts described inside)

I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.
The project is split over 4 .java files all in the same package.
My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).
I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:
javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java
This creates 4 corresponding .class files fine.
The Main is in bibliotecaApp.java/class, so I try run it by:
java BibliotecaApp
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)
Plus about 13 lines of specifics.
Now googling this error seems to be a class path problem, and this is where I get stuck.
From places I've read, usingecho $PATH gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is still from the directory of the .java files above.
That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.
That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:
java -jar biblioteca_jar
I get
Error: Invalid or corrupt jarfile biblioteca_jar
Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?
The code is on my GitHub if that helps anything
https://github.com/botagar/twu-biblioteca-JohnGeddes
Based on your compiler step, change this
java BibliotecaApp
to
java -cp . BibliotecaApp
Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.
A jar file is a kind of zip, and should have a .jar extension. So this
java -jar biblioteca_jar
should probably be
java -jar biblioteca.jar
And you can check if the file is valid with any zip archive reader. Including jar itself,
jar tvvf biblioteca.jar
Edit
Based on your comments below,
cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/
and then
java -cp . com.twu.biblioteca.BibliotecaApp

Executing .jar file from command line cannot find the configuration file in the root folder

I wrote an application which starts by reading a configuration (.ini) file and accordingly parses some xml files within a particular folder.
Everything works perfectly when I build and run my application in NetBeans. Now I wanted to run my application through command line (because that's actual requirement of my project), so I am executing the .jar file created by Netbeans to run my application.
I am executing the application as such: java -jar Application.jar.
The issue is when I run this command on command line, the application seems to not be able to find the configuration file which is in the root folder of the project. Its really odd because NetBeans runs it perfectly.
I believe the reason I am getting this exception is because to run the .jar file I am changing my directory to dist and then running the java -jar command and therefore the file is not being found . But then how do I get around this.
Any help is appreciated. Thank you
Sorry got it figured out. I am executing jar file from the root but adding the source in the command as well.
Sorry, you can close this thread.
I had this problem till I installed JRE and did NOT use stand alone.

Can't find or load main class JarSplice

Hello fellow programmers! I am writing a game at the moment, that uses the following libraries: LWJGL, OpenGL, Slick. I know the problems with simply exporting a JAR file from eclipse when using libraries like that, and I do use the JarSplice program to make a fat JAR. This has worked fine for about a month now, however, just recently it stopped working. Whenever I export the JAR from eclipse, and make it a fat JAR using JarSplice, I get this error:
Could not find or load main class Game.jar
META-INF (Located in the fat JAR):
Manifest-Version: 1.0
Launcher-VM-Args:
Launcher-Main-Class: cubix.main.Cubix
Main-Class: org.ninjacave.jarsplice.JarSpliceLauncher
Please note that the main method of the program is located in the Cubix.class, which is located in cubix.main.Cubix'
I have never came across this error before, and it is driving me insane. If any additional information is required, please feel free to ask.
EDIT:
I might also add that if I export any other project this way (using the same libraries and using jarsplice to build a fat JAR) it works just fine. I am only experiencing this problem with this specific project in eclipse. However, when I run Cubix in eclipse debugger, it runs just fine.
Try couple of things :1 check the content of the file, use jar -tf see everything is present2. can you try running this file from command/terminal.3 if all above is good, compile Cubix.java from terminal and place this .class file in jar file manually and check.
I'm sorry for posting this. The problem wasn't that it couldn't find the main class but rather I had typed the command line wrong. I typed: 'java Cubix.jar -jar' rather than 'java -jar Cubix.jar'. Also, after fixing the command line, it gave me the stack trace for the error that was overlooked by eclipse, and that one of the textures that needed to be loaded was case sensitive.

Categories

Resources