i am making a program in which i have to allow the user to browse an image and store it with its details in image folder inside the source cod
my code is working working in netbeans but when i make jar file the code is cot able to store image.
i have stored some images through netbeans and able to access them using image/imanename.jpg but can't able to store images.
help me as soon as possible. thank you
the code i tried is
File f = new File(s);
long size=f.length();
FileInputStream fis1=new FileInputStream(f);
FileOutputStream fos2=new FileOutputStream("image/"+tfpn.getText()+".jpg");
byte b[]=new byte[10000];
int r=0;
long count=0;
while(true)
{
r=fis1.read(b,0,10000);
fos2.write(b,0,10000);
count = count+r;
if(count==size)
break;
System.out.println(count);
}
System.out.println("File copy complete");
When you generate a jar file, it is in a compressed format (like .zip). You cannot simply save a file into the jar. It is technically possible, but is complicated. See here for more info.
The reason it works in netbeans is because when you run a file in netbeans, the code is not yet compressed into a jar. Once you make the jar file, you cannot simply write files into the jar. Try and save your file in a normal directory (not within the jar).
Related
I want read variable value from another jar
Main.jar
public static int version = 2;
I already did add libraries navigate to Main.jar (lib/Main.jar)
then I do this from Loader.jar
int version = dummy.Main.version;
Do replace with a new one if there are update
Loader.jar
URL url = new URL("http://127.0.0.1/cn/Main.jar");
try (InputStream in = url.openStream()) {
Files.copy(in, Paths.get("lib/Main.jar"), StandardCopyOption.REPLACE_EXISTING);
}
catch (HeadlessException | IOException | NumberFormatException e) {
//do exception
}
}
But the problem is i can not replace file because the file is being used, since Main.jar is used by Loader.jar
How the solution to replace the file being used ?
Don't replace the JAR file, because you cannot safely force the other process to not be using it.
Instead, install the newer JAR file next to it, with the version number embedded in the JAR file name, and rewrite your other users of the JAR file to periodically scan for newer JAR files. When finding one, they should throw away the class loaders that loaded the old JAR files, and reload the new set.
Or, decide not to embed this information in a JAR file at all. Resource files are easier to read and dispose of than JAR files, and they are plain text files.
You have some options
close the program using it and try again after restarting.
create a new JAR file each time and load it in a different class loader each time.
read the contents of the class in the JAR using a byte code reader such as ASM.
instead of storing the version in code, store it in a file (or as well). You can easily read the file from a JAR without putting it on the class path.
run javap to dump the code for the static field and extract the value from the text.
I suspect the last option is easiest to code.
I have created a java program that other testers will use to help with their testing. I will be sending them a zip file with the .jar, a readme.txt, and main.properties.txt file.
The main.properties.txt file is a template for the testers to input their DB access credentials. They will update the main.properties file with their db cred's and then attempt to run the .jar from the terminal or command line. The issue I am running into is this. My program needs this updated main.properties.txt file so it can create the connections to our DB's.
What instructions do I need to give in my readme so my program can successfully find the main.properties.txt? Does the main.properties need to be in the same directory as the .jar? Can the testers just create a file on their desktop or documents folders to put the .jar and main.props?
The other question I have is how do I pass this file to my program once its ran from the terminal? Currently it is really easy, because the main.props is part of my program and I can just do something like
Properties prop = new Properties();
FileInputStream in = new FileInputStream("src/main/resources/main.properties");
prop.load(in);
in.close();
But now main.properties is not part of the project anymore. I don't know how to change the code above so that it can find the text from a directory on the local. The location in which they wish to put their main.properties is out of my control so writing a static path will not work. Please help!
There are many ways, I'll show you two.
You need a File object that points to the main.properties file. Then you create a stream on this object new FileInputStream(File) , as you already did by using a String.
The problem of course is to get a relative path to main.properties.txt which works on all systems, regardless where the jar-File is located.
1. Desktop
In this case the main.properties.txt is located at the users desktop. Here is how you access it:
File desktop = new File(System.getProperty("user.home"), "Desktop");
File target = new File(desktop, "main.properties.txt");
Alernativly, if you plan to distribute configuration and property files that do not require user interaction, you may want to use locations like Temp or Documents (Windows).
2. Relative to the jar
Probably one of your best options. Assume the target is in the same folder than the jar-File (or at least in a fix structure relative to the jar). Here is how you access it (related question: how-to-get-the-path-of-a-running-jar-file):
CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File jarDir = jarFile.getParentFile();
File target = new File(jarDir, "main.properties.txt");
I have built my GUI program and got a runnable jar file but I want to include a txt file that can be edited by the user to the jar file or accompanied with it. this text file can contain a path of an editor that is different between operating systems. Is there a way to do that?
thanks
you cant edit a file and save it inside the jar. what you can do is include your file in the jar with a default data, edit it and save it to a file outside the jar.
actually it is possible if you open the jar itself with the code, and repack it when you change the text, but its like disassemble a car doors, wheels, seats and engine just to put something in the glove box
The simplest way is to place the .txt (eg. file.txt) file by the jar, i.e., you can access it in your Java code:
File file = new File("file.txt");
In another way, if you want to launches the associated editor application (default for that file type in the current OS) for editing, you can try the next:
File file = new File("file.txt");
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().edit(file);
}
So I've created a game, runs perfectly in eclipse--all of the images load and it reads properly from a text file. The text file provides level info.
This is where my problem comes in.
I exported it all to an executable .jar file, and it found all of the images. But it did not find the .txt file. Where does the .txt file need to be for it to be found?
try{
controlBoard = new Scanner(new FileInputStream("lvl1.txt"));
}catch(Exception e){
gb.error = true;
}
You should not access that file using FileInputStream. Get an InputStream using the class loader and getResourceAsStream(). It'll find it in the JAR.
I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.