The Java application works fine on Eclipse, but it does not work properly after deployment as an executable jar

Like the tile, my Java application does not work, as it works great when developing on Eclipse.

I have finished creating and testing my application on Eclipse. It works fine as I expected. I needed to deploy it as a running jar so that my client could not use it in their system. I made it a running bank by exporting it to Eclipse. When I run the runnable jar, the application starts working normally for a while and stops when reading the file. I had no problems with this code on Eclipse.

The original snippet, which does not seem to work, is as follows.

sfis = new SmbFileInputStream(sFile); in = new BufferedInputStream(sfis); byte buf[] = new byte[(int)sFile.length()]; int pos = 0; int size = 10; int temp; while((size=in.read(buf, pos, size)) > 0){ pos += size; temp = buf.length - pos; if(temp < 10){ size = temp; } } 

In Eclipse, this does not cause any problems. It reads data perfectly from the SMB connection and shuts down. But from the jar application, it seems to stop reading from the input stream inside the while loop at some point. This is a very strange case that I have never experienced before. And I can not find a solution for this.

Is it because of my code or jar file that may be incorrectly made by Eclipse?

---------- Additional Information -------------------

With a lot of help from Edmondo1984, I find out where the jar program stops. When it goes to the input stream from SmbFile, a new jcifs.util.transport.Transport stream is created, and the stream is simply blocked, and the application creates another jcifs.util.transport.Transport file, etc. By creating an 8 or 9 jcifs.util.transport.Transport stream, it gets stuck and does nothing.

Same code, same jcifs library. But it works differently between running on Eclipse and the local computer as an executable jar. I have no idea why this is happening.

+4
source share
3 answers

The return value for "no longer in the stream" is -1 (see Javadoc ).

Imagine what happens if you try in.read(buf, pos, size) , but basic information is still not available. Then the call returns immediately with a return value of 0 (zero). Thus, the loop condition is evaluated as false, and the loop terminates. Therefore, I think you should compare with != -1 .

Another possible problem: you can also think about what happens if size = 0 (for example, due to the above script) and temp >= 10 . Since you are directly returning the value of size to in.read(buf, pos, size) as the maximum number of bytes read, you may end up in an infinite loop, assuming you have already adjusted the comparison with != -1 .

+2
source

When interfering with flows, these behaviors are common. Small algorithmic methods can cause various symptoms in different environments.

You can print "debug" information on the console to better understand where and why your code has stalled. I'm sure he honored the method.

There are several errors in the code, the most notorious block:

 temp = buf.length - pos; if(temp < 10){ size = temp; } 

Why? When 10 or fewer bytes are left, you will try to read them in the next read (), otherwise you will try to read too much (the entire file size again). Running in Eclipse is probably the file being read in the first read (), apparently not from Eclipse. Try changing the code to something like this:

 FileInputStream sfis = new FileInputStream(sFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = new byte[4096]; int read = sfis.read(buf); while(read>0){ baos.write(buf, 0, read); read = sfis.read(buf); } sfis.close(); 

It is also recommended that you use Apache Java IO utilities instead of always writing this type of code. Java IO requires a lot of templates, and a good programmer has the best features.

On a side note: reading a file into memory is usually bad. If you are not going to somehow transform your data as a whole, streaming is always better. I don’t know what your program is for, but keep this in mind: what happens if you try to read a 10 GB file? or several?

Happy coding,

+1
source

Java is an uppercase letter that is sensitive in many ways. For example, if you have a file called image.PNG, and in your code you set the location to "/image.png" (instead of "/image.PNG"), it works fine in eclipse, but after exporting the java executable jar file VM will not be able to find this file.

0
source

All Articles