I have a program that I want to publish that was built using Java. At the moment, the program creates folders / files in the directory:
"C:\Users\Steve\Documents\myProgram"
This obviously won't work unless the users have the exact same folder structure. How do I direct the program to a folder on the desktop? As the path "C:\Users\Steve\Desktop" wouldn't work.
Publishing the program using NetBeans creates a .jar. As I want the program to be used by many people, is it possible to create a batch file that using commands:
Creates a folder onto the desktop (that stores the program files created through the program)
Then launch the .jar
I could then get a converter and change the .bat to .exe. I'm not sure if this is the correct way about it.
On a side note, I'm pretty sure this wouldn't work as the user would still need Java installed to run the program. I'm not 100% sure, hence me asking.
How do I direct the program to a folder on the desktop?
File desktopDir = new File(System.getProperty("user.home"), "Desktop");
File myFile = new File(desktopDir, "myFile.txt");
Related
Friend cannot open an exe I made from a jar file. Gives a java exception, assuming that simply means he doesn't have the correct version of java, is there a way for me to include the files needed to run it with the exe, to remove the need of having java downloaded?
I made a java program with jframe for a friend to help him do some calculations and I was able to convert it into an exe but when he downloaded it and tried to run it, it gave a java exception, is there a way I can include the java files needed to run the app with the exe?
You have to first create a .jar file and then convert it into an .exe file. You can refer to this link https://medium.com/#sulabh4/how-to-make-a-executable-file-from-your-java-code-3f521938ae5c
I’m doing a little Java project that deals with files (ppt files ), and I want to incorporate this files in the program (jar) so that those file can be open on every computer without having to add them all the time.
I have try adding them to the source file but it doesn't work they don't open.
It's there a way so that once I create the jar of the project I can run it always (with the file in the jar ) ?
I have a Java program that I've made which opens and uses .project files. As long as I open the files from within the program everything works fine. However I want to be able to install the program and register (maybe even maintain) file association so that when the user will double click on a .project file one of two things will happen:
If the program is not running, the program will launch and will have the file path (complete with name and extension) passed to it so it could open the clicked file.
If the program is already running, do not open a second instance of the program but rather pass the file path (complete with name and extension) to the program so it could open it.
I've scoured the internet and found things such as:
The Java™ Tutorials
and
Stack Overflow - inno setup file association
but these answers are confusing. They talk about using external programs (is this necessary?) and don't explain properly how to accomplish file association in them or talk about Ant code while building in Netbeans (which I am using) but I don't really understand how to start Ant code, if I need to add it, add a new .xml file to the project or whatever.
I've tried manually associating my built JAR file in the Windows association list but it will not accept it. I am guessing I need it installed and/or converted somehow into an .exe.
In any case I want to distribute the program to others and so it needs to be done automatically.
I would prefer to have this work on multiple platforms but I am most concerned with this working on Windows (Windows 10).
I am running Windows 8.1, Netbeans and Java 7. I have a jar file sitting in the dist directory, on the "C:" drive. (Netbeans automatically creates a "dist" directory to store the jar files it deploys.) When I run the jar and specify the "dist" as a target directory, my program is able to create files in "dist" using "createNewFile". (My program can also read from and write to the new created files).
My problem is that my program does not seem to be able to create files elsewhere on
the C: drive, under Windows 8.1 (It is able to do so under Windows XP.) To be more specific, createNewFile does not create a file, does not throw an exception, and a System.out.println ("Message") statement placed directly after the createNewFile statement does not execute. No errors are reported anywhere. I have tried to deactivate User Account Controls but no avail.
Does anyone know directories in Windows 8.1, outside of "dist", to which my program could
successively write files and this without having to call upon administrative privileges? Thank you.
I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS.
Eventually it will ship as a runnable jar with some config files in the same folder. To load them I need the path to the folder the jar is stored in within the program.
There are already a couple of threads like this one or this one.
My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so:
java -jar /path/to/jar/jarfile.jar
However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. (MacOS users report the same issue)
Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal):
ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./
System.getProperty("user.dir");
Output: /home/myusername
Is there any other way to do it or am I doing something wrong when exporting the jar? Any help would be appreciated!
Cheers
Nick
Make it simple, and use a startup script (.bat/.sh file) to run your application. This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. This has the additional advantage of being able to pass other arguments, like the size of the heap, etc.
On windows, %~dp0 is the path of the directory containing the executed bat file. On Unix, you can use $(dirname $0).
You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/".
Painful as it is, the Preferences API, Preferences.systemNodeForPackage, seems the wisest alternative, if there is little structured config data. There is an inputStream method for import; your initial config template could be a resource in the jar.
Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. Note that path separators are OS dependent (File.pathSeparator)