This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Executable jar from Eclipse

 
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have generated a runnable .jar with Eclipse I am able to execute it from the Linux command line correctly.

When I click twice over it just open but it doesn't run. I am using a 64b system.

Is there a possibility of making it run with just double click in both Windows and Linux?

 
Ranch Hand
Posts: 218
5
MS IE Notepad Suse
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When executing from file explorer (that's what you mean by "double click on it") it's about file-(extension)-type association, or easier: What yor file explorer is configured to do when it encounters a specific file type. Most file types define a filename-extension and a magic number header, both can be used to determine what type a file contain.

As JAR files are just ZIP files following some special rules, it might happen your system opens a jar with a zip capable archive manager instead of launching it with java.
Also, at least on windows, jar files are executed with javaw executable wich doesn't open a console window (that's the famous "nothing happen" problem). By modifying this to use regular java executable wich does open a console window most beginners encounter "a black window only shortly pops up and closes itself" issue wich is caused by java display an error message and terminate itself. To read the error message beginners often told to manual open command prompt and run java by hand so the console window stays open and the error message can be read.

You wrote you can run you application from command prompt - how you do it? Do you give additional parameter without your code doesn't work? Do you have classpath dependencies?
Analyze what happen when you try to run your archive from file explorer and it differs from when you run it manual from command line. Most issues are easy to fix.
Also: do you have more than one version of java ? Could be the runtime bound to double click is different from what you start manual.
 
Saloon Keeper
Posts: 21266
138
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can make the JAR execute properly from the command line (Windows or Linux) with the command "java -jar myjarname.jar", then the JAR is as executable as it gets.

To make it possible to execute by double-clicking on an JAR icon, each and every computer that the JAR is installed on must have its default desktop preferences overridden. Because, as Matt said, the default action when you click on a JAR file's icon is that the OS is going to look for a ZIP display application and open the JAR with that application and not with the Java runtime (JVM).

Since that's a rather extreme measure, a lot of executable-JAR apps are distributed with an accompanying shell script(s). These scripts are simple Windows .BAT files and Unix shell scripts whose sole action is to launch the JAR using a command format like the one I just gave. Maybe they'll also select which installed JVM on the user's computer to run under, but that's generally about it, unless it's a really complex app like the Tomcat server.

Since the default action on most user's GUI desktops when they click on an icon representing a shell script is to run that script, no overriding of the user's desktop settings is needed. Note, however, that on Unix/Linux/MacOS systems, a script won't execute from the GUI unless its "x" (executable) file attribute is set.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The app is able to send an email, it works correctly when the .Jar is run in Linux but then when it gets run in Windows I get the error;

The path in the error has been updated by this one before package the .jar in Eclipse (Linux CEntos) and then run it in Windows. It looks like this;

How is possible that it takes the old path?
 
Ranch Foreman
Posts: 53
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, to run the topic:

Your initial issue is caused by your application can't find the file specified. Hence I have a few tips for you when dealing with files:

1) When ever using filenames, always use forward slash "/" as separator. Java offers the convenience abstracting file access. So, it doesn't matter on wich platform your code is run, Windows, Linux (include MacOS), Android, embedded, etc., Java internally translates what ever you give it to a valid path based on local filesystem rules. Also: It gets rid of all those double backslashes and reduces possible typing errors when only one or three backslashes made it into the string and still compile as a legal string. So, it could slip in a simple \r, \n, \t, or any other valid escape sequence. Always using forward slash mitigates this.
2) Although this might be a personal preference, it helps when always dealing with absolute paths. Java is deterministic in the way paths resolved when given relative. Unless data inside the current jar are access, wich are always relative to its root, it can throw up when using relative paths inside and outside IDE environments.
3) Avoid UNC paths. Java just delegates UNC the be resolved by the OS instead of implementing the SMB protocol itself, it still should be avoided as you might run into access issues. Always let the OS provide a local path (using mount net share dirves on Windows or mount on Linux).

For your application I would suggest this:

Is the resource an asset used by your application, like a footer or signature, or is it message specific?
When it's the first use case, I would suggest import the resource inside the jar and load it with getResourceAsStream(). Otherwise, put it extern and set an absolute path to it. Another idea would be using a JFileChooser (maybe in a loop) to manual select attachments.

In addition, it may could help to post some lines of code so one could look over it.

Hope this helps and keep asking.

Kris
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have replaced the slashes to "/" and I have make sure that there are only relative paths...but still I ge the same error it is like at some point the app is searching for the old Linux path.

 
Rancher
Posts: 4325
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So where is that path being logged?
Where is that value appearing?

You need to find where in your code that value appears.
 
Kristina Hansen
Ranch Foreman
Posts: 53
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:I have replaced the slashes to "/" and I have make sure that there are only relative paths...but still I ge the same error it is like at some point the app is searching for the old Linux path.


Seems there is a mis-understanding here. But we can try to get this:
The filename suggest, that this seems to be either a message specific file or something auto generated (such should name "latest" or something and maybe is only a symbolic delegate to a file with proper naming). So, as it seems an external resource outside the jar, try set an absolute path.
May read agian: I've wrote: "avoid relative and use absolute paths".
Furthermore the code you posted doesn't contain any line access such resource. It's just a simple text/plain message. That's not helpful to analyze your issue.
Also you mentioned, that its still try to lookup some old path. That seems to be a refresh issue so what you get as output doesn't fit your source. In an IDE try "clean project" to complete rebuild based on current source.

Sorry this post isn't that helpful, but there're a few more information needed from your side here.

Kris
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What happened is that the idea is that all the resources are contained in the .jar in order to ship all together thats why I think that using absolute paths would not be a solution.
 
Kristina Hansen
Ranch Foreman
Posts: 53
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:
What happened is that the idea is that all the resources are contained in the .jar in order to ship all together thats why I think that using absolute paths would not be a solution.


That's right - in this case you have to use relative paths and Class.getResource() or Class.getResourceAsStream() to access data within the JAR.
I just replied cause you didn't mentioned that yet.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to find where in your code that value appears.


Ths thing is that it doesnt appears in the code.
 
Kristina Hansen
Ranch Foreman
Posts: 53
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:

You need to find where in your code that value appears.


Ths thing is that it doesnt appears in the code.


Well, then it's really strange how this exception is thrown. Please post to complete stacktrace so we can try figure out where it gets thrown so you can post the code of this method.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is error trace like it

The current code it is the one I posted in the previous post the past code was containing a call to that path but not now.
 
Dave Tolls
Rancher
Posts: 4325
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is (based on earlier stuff you've posted) trying to find a file you have included inside your HTML message for inclusion in the email.
What code is building that message?
Specifically what code is building that path?

ETA: scratch that, I see where the suffix for the path comes from in that DataHandler.
 
Dave Tolls
Rancher
Posts: 4325
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, got it:



It's that <img> tag.
The parser is trying to read that file so it can be included as part of the email, for inlining the image.
 
Kristina Hansen
Ranch Foreman
Posts: 53
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spot several issues in your code. Let's try to go this one by one:

When you want to use files inside your JAR you have to use Class.getResourceAsStream(). A FileDataSource can only be used for files in the local filesystem. Data inside the current application JAR can't be accessed as FileDataSource doesn't support JAR access.
Also: your img-tag looks wrong. You set a local path (wich for security resons should be blocked by any sane modern mail client). This doesn't work. When referencing inline images in HTML mails wich are added as attachments you have to specify the path to point to the attachment. I didn't worked with HTML e-mails myself yet, only know this from a program I use at work wich displays HTML mails a raw text. There should be some information available on google how to do this.
Next is the path you try to use: relativ paths for access files inside JAR have to use unix-style paths with "/". But as I look through JavaMail API doc I'm not sure if it can handle such data. Maybe you just have to use filesystem paths. This can be done by extract data from JAR at runtime and use extracted file.

There's a lot of work needed to get this working as intended.

Maybe I find the time to put an example together for you to work of it.

Hope this kind of helps.

Kris
 
Kristina Hansen
Ranch Foreman
Posts: 53
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, although I'm quite firm with mail api as I fiddeld around with it the last few weeks, it took me some time and google to get the example working as you want it.

Both the class and the image are only inside the JAR, no external file in the regular file system.
Here's the working code (stripped credentials):

I've build the jar myself with this command:

and executed it from an unrelated directory (to make sure to only use the jar)

The mail I received from my mail server at google looks like this:

As I tested around I figured that it's important to set the outer MimeMultipart to "related". If you don't set it, it gets default "mixed", wich causes the image not to be displayed and the content-id to be set to "<image>" instead of "image". Only if set to related java-mail correctly figures both parts belong together and the mail gets displayed correctly.
I also figured your initial issue: You tried to use FileDataSource - wich, as doc says, only wraps a File, wich doesn't work when like you want only data inside the JAR to be used. File always point to a regular file in filesystem - it can't be used to point to logical files inside a JAR. That's what getResource makes a URL as a URLDataSource can also handle the JAR URL scheme allow java to access data inside a JAR.

Hope this helps to get your code running.

Kris
 
Evacuate the building! Here, take this tiny ad with you:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!