I made the following code to put in pictures to my java program:
private String ICET = "." + File.seperator + "Bilder/ICET.jpg"
//some code
label.setIcon(new ImageIcon(ICET));
//some code
It does, that the label has an IconImage and it works in every Workspace with a folder "Bilder", which is situated in the same folder as the program.
It works as a jar-file too, but if I convert the jar-file to an exe using launch4j, the program does not show the pictures.
use
new ImageIcon(getClass().getResource("path"));
path -> if is same folder put only name image : "ICET.jpg"
Most likely, the working directory set when started as an exe differs from when it is started as a jar-file, and thus your program looks at the wrong place for your "Bilder" folder.
You could try this out by printing the absolute path for it, e.g. like this:
System.out.println(new File(".").getAbsolutePath());
To fix the issue, you might try to set the chdir-option in the launch4j-config to . (a single dot). According to the docs:
<chdir>
Optional. Change current directory to an arbitrary path
relative to the executable. If you omit this property or leave it
blank it will have no effect. Setting it to . will change the current
dir to the same directory as the executable. .. will change it to the
parent directory, and so on.
But in the end, you should load the images from the class path, and not just from a folder. If you read them from the class path, they should hopefully be integrated into the exe, otherwise they are not and might get missing when users copy to exe to a new location, making your program no longer functioning.
Related
This question header isn't the best, but I am trying to create a directory from the person's computer in java, then when a button is clicked, to navigate to that created directory and create a file. I just need to get the path then create the directory. Here is a sample:
File theDir = new File("Users/" + System.getProperty("user.name") + "/InfoSaveFiles");
if (!theDir.exists()) {
if (theDir.mkdirs()) {
}
}
The only problem is that when I run this, (from a compiled jar file or from my IDE), it just creates a directory Users/(name)/InfoSaveFiles instead of navigating to that particular path and creating the directory InfoSaveFiles as I would like.
How can I make it go to the specified path, then create the directory?
If you want to are using that file path, it will look for Users/(name)/InfoSaveFiles in the present working directory, in case of IDE, the root of the project file. So, if you add a slash / before it, like '/Users/(name)/InfoSaveFiles`, then it will look for the hierarchy from the root of the current heirarchy. In case of windows, from the drive you're running the program.
For example, if you set the file path with slash before it and you're running the program in the E:// drive, then the program will look for Users/(name)/InfoSaveFiles in that drive, and will create the hierarchy if not exists.
So, if you want to do that in specific drive, you need to mention that too at the front of that file path.
For Linux, starting with / indicates the root directory. So you can achieve it in linux giving the current file path you've mentioned with an added / in the front. It will look for that path from the root directory then.
Hope that answers your question.
I would like to write JSon files in my Java Application, it works perfectly in my IDE but as soon as I move the directory, the system fails to write the file. I eventually found the problem : The line System.getProperty("user.dir") returns a bad path.
The files are located in C:\wamp\www\myProject during development. Here is my code at the moment :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\wamp\www\myProject\src\json\Crc.json"
After moving my project to C:\Users\myUser\Desktop :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\Users\myUser\src\json\Crc.json"
I would like to have a way to find where my project directory is on the computer at anytime. Do you guys have a solution ?
The C:\wamp\www\myProject folder is just a random place on your hard disk.
Java supports 3 generic places to look for resources:
the current working directory. this is where your command prompt is and what System.getProperty("user.dir") returns, but you cannot rely on that beeing somehow related to any cretain place in the file system, especially not related to the project structure on your own system.
You should only use that if your program has a command line interface and looks for some default file name to work with.
the user home This is what you get when calling System.getProperty("user.home"). On Unix this resoves to $HOME and on Windows to %USERPROFILE%.
This is the best place for files changed at runtime or holding user specific content.
the own code location. Resources in the same package as your class are accessed with getClass().getResource("filenameWithoutPath") But usually you place resources in a special folder in the application root and access it like this: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath").
In your IDE this special folder should be Project/src/main/resources (according to the maven Standard Directory Layout
This is appropriate for some global configurations that you change when creating the delivery package.
I'm self taught, and while trying to load an image with getClass().getResource(path) I think I've highlighted a problem with how I structure my projects. I currently just use cmd and TextPad.
My folder structure is as follows:
Desktop/Java/uk/co/woodward/recruit
/build
/images
/src
I compile from the Java folder in command prompt using
javac -d "." uk/co/woodward/recruit/src/example.java
The .java files are in package uk.co.woodward.recruit.build;
This all works fine and classes end up in the build folder, but while reading how to use icons in the java trail I realised that "build" isn't a package and I'm a little confused about how to set the location of an image file relative to the current class. I'm also unsure of whether I should set 'Java' (or recruit) as a root variable/path of some sort, rather than just compiling from there.
Anyway, nothing I've tried so far has worked.
public CandidateFormToolBar(){
saveButton.setIcon(createIcon("/images/save.png", "Save"));
add(saveButton);
}
private ImageIcon createIcon(String path, String btnName){
URL url = getClass().getResource(path);
if(url == null)
System.err.println("\nThe " + btnName + " Icon Path Cannot Be Found: " + path + "\n");
return new ImageIcon(url);
}
When running the program I've tried having the image in the build folder with the classes and using the path "save.png", I've tried the above code (which I thought would go up a level to the recruit folder and then to the image folder), with no luck. I've tried using -cp to point the jvm to the images folder. And much more.
And the outcome is that I've realised I still don't understand this well enough, and that my structure probably isn't recommended. Which is slightly depressing!
If anyone could point me vaguely in the right direction it would be appreciated!
Put your images inside the folder where you have put your Java files. Like in case of tomcat server this is the structure I use for accessing images in my jsp files.
>>Web content
>imagesfolder
>Web-inf
Myfile.jsp
Now in this Myfile.jsp file for accessing images from imagefolder I use following url "imagesfolder/myimage.png". Here Web content is the folder which have Myfile.jsp and 2 sub folder imagesfolder and web-inf and imagesfolder contains myimages.png
You can also make a directory like this if your file are in Java folder then create a image folder in that and access them using the url pattern mentioned above.
Hope this solve your query.
This is my first Stackoverflow question.
I searched across Google for getting the current file name in Java. Most of the sources tell users how to find the current file name if the file is a JAR file, but I'm asking for if the current file is an EXE file.
I saw one EXE answer in Get name of running Jar or Exe, but I don't think it worked.
I use the JSmooth EXE wrapper (launch4j somehow didn't work for me), and Java 8. Is there a straightforward solution to my question? Also, it's nice to explain how it works, or provide a link to the Java documentary about it.
EDIT: To clarify, let's say that I made a Java program and used a JAR wrapper, and I named the resulting EXE "test.exe". I want the Java program be able to give me the current directory of "test.exe", including the filename itself (test.exe).
ANOTHER EDIT: Just to clarify more, go onto your desktop, create a text file, and put some text in it. Save it, and change the text file to an EXE file. Then, try to open it. Windows will give an error. Notice how the title of the message dialog is the file path of the opened file. That is the type of output I want.
Thanks.
Per the JSmooth documentation,
JSmooth also makes some special variable accessible for your application.
Form Meaning
${EXECUTABLEPATH} Replaced by the path to the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable
is replaced with c:/program files/jsmooth
${EXECUTABLENAME} Replaced by the name of the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable is
replaced with test.exe
You set these in JSmooth under the "Environment Settings" (the last panel), which allows you to map the variable name. So,
MY_EXECUTABLEPATH=${EXECUTABLEPATH}
MY_EXECUTABLENAME=${EXECUTABLENAME}
In your application, you can get those with
String execPath = System.getProperty("MY_EXECUTABLEPATH");
String execName = System.getProperty("MY_EXECUTABLENAME");
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
}
}
This will print a complete absolute path from where your application has initialized. It is used to get only the DIRECTORY.
If you are using a .exe why do you not create a installer? You can use Inno Setup, this way you are able to specify where do you want to store your .exe, and get it from your application just passing your custom directory
I've written a simple java app, say, with the following code:
String currentDir = new java.io.File(".").getCanonicalPath();
javax.swing.JOptionPane.showMessageDialog(null, currentDir); //This line shows a graphical dialog with the current dir
When I run it through the terminal, it gives me the directory where the jar-file is located. But when I run it using the GUI file manager (that is, right click on the jar-file -> Open With -> OpenJDK Java 7 Runtime) - the working directory is my user home directory (/home/angstrem). How can I set the working directory to be the one, in which the jar-file is situated?
You can do this:
String jarPath = YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
replacing YourClass with an actual class defined in your jar.
You can then make your file paths be relative to jarPath, and your program will work regardless of its working directory.
You can't, and you shouldn't be able to. Consider multi-threading for example. You can only make your application current-path-insensitive.