Want to Make a Jar file - jar

I've made a java application(it extends JPanel) and want to make it into a jar file.I have BlueJ and I used it to do so but after I make the jar file it doesn't open. Can anyone help?

Jar files are PKZIP packaged files which have specific folder structure and convention in it. It contains the compiled version of your java sources (*.java -> *.class) plus meta data. You can "open" it with any archive which can handle zip files especially if you append the ".zip" extension to the file name.
If you mean executing your jar by "opening" it, that's a different thing. You have to have a standard static main method in one of your classes so the JVM can find the execution entrance point. If you have that you can say "java -jar yourjarfile.jar", which will execute your class starting from your main. Note, that I assume that you have JVM setup well (I guess you have) and your environment variables should be configured too. In most cases, your jar refers to other jar files. You can provide access for those for the JVM through classpath.

Do you use Eclipse to write code? If you do, then it is easy to use Eclipse to create an executable jarfile.
Do you use a JFrame to present your JPanel? If you do not, you may have a JFrame, it is the "application window" where the JPanel will be presented.
JFrame tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
/Johan

Related

Javafx project deployment - how to handle assets/resources?

I recently finished my first JavaFX project and am ready for deployment. I found that when I create the jar file for my project my ide creates a folder with the jar and other necessary files. I noticed that when I run the jar in the file everything works fine. However, when I take the jar our of that folder and place it as a desktop icon, various resources no longer become available - I am assuming this is because the jar file and the various resource files are no longer in the same file path/folder. - Is this the correct assumption to make?
Regardless, I wanted to ask what is the standard method of getting JavaFX resources and the accompanying jar file to work when the resources are not located in the same folder?
Essentially, I want to have a clickable desktop icon that launches the app, which the jar file fulfills. But if I put that jar file in a folder with its resources to get the project to work properly then the user will have to press the folder and then the jar file in order to get the project to launch - which is very counterintuitive.
Any ideas on how this issue is handled?
The best way to do this is to create a shortcut to your jar file not copy it to another location. The jar file depends on these resources to execute especially if you used external libraries.
The other alternative would be to export your jar file with the libraries included in the jar. This however would make your jar very huge depending on the number of libraries you have.
I hope this helps.

java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver [duplicate]

How should I add JAR libraries to a WAR project in Eclipse without facing java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError?
The CLASSPATH environment variable does not seem to work. In some cases we add JAR files to the Build Path property of Eclipse project to make the code compile. We sometimes need to put JAR files inside /WEB-INF/lib folder of the Java EE web application to make the code to run on classes inside that JAR.
I do not exactly understand why CLASSPATH does not work and in which cases we should add JARs to Build Path and when exactly those JARs should be placed in /WEB-INF/lib.
The CLASSPATH environment variable is only used by the java.exe command and even then only when the command is invoked without any of the -cp, -classpath, -jar arguments. The CLASSPATH environment variable is ignored by IDEs like Eclipse, Netbeans and IDEA. See also java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable.
The Build Path is only for libraries which are required to get the project's code to compile. Manually placing JAR in /WEB-INF/lib, or setting the Deployment Assembly, or letting an external build system like Maven place the <dependency> as JAR in /WEB-INF/lib of produced WAR during the build, is only for libraries which are required to get the code to deploy and run on the target environment too. Do note that you're not supposed to create subfolders in /WEB-INF/lib. The JARs have to be placed in the root.
Some libraries are already provided by the target JEE server or servletcontainer, such as JSP, Servlet, EL, etc. So you do not need put JARs of those libraries in /WEB-INF/lib. Moreover, it would only cause classloading trouble. It's sufficient to (indirectly) specify them in Build Path only. In Eclipse, you normally do that by setting the Targeted Runtime accordingly. It will automatically end up in Build Path. You do not need to manually add them to Build Path. See also How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and JEE libraries which are not provided by the target servletcontainer (e.g. Tomcat doesn't support many JEE libraries out the box such as JSF, JSTL, CDI, JPA, EJB, etc), need to end up in /WEB-INF/lib. You can just copy and paste the physical JAR files in there. You do not necessarily need to specify it in Build Path. Only perhaps when you already have it as User Library, but you should then use Deployment assembly setting for this instead. See also ClassNotFoundException when using User Libraries in Eclipse build path.
In case you're using Maven, then you need to make absolutely sure that you mark libraries as <scope>provided</scope> if those are already provided by the target runtime, such as JEE, Servlet, EL, etc in case you deploy to WildFly, TomEE, etc. This way they won't end up in /WEB-INF/lib of produced WAR (and potentially cause conflicts with server-bundled libraries), but they will end up in Eclipse's Build Path (and get the project's code to compile). See also How to properly install and configure JSF libraries via Maven?
Those JARs in the build path are referenced for the build (compile) process only. If you export your Web Application they are not included in the final WAR (give it a try).
If you need the JARs at runtime you must place them in WEB-INF/lib or the server classpath. Placing your JARs in the server classpath does only make sense if several WARs share a common code base and have the need to access shared objects (e.g. a Singleton).
If you are using Maven:
Open the project properties, and under Deployment Assembly click Add...
Then select Java Build Path Entries and select Maven Dependencies
Resolved by setting permissions.
Had related issue using PySpark and Oracle jdbc. The error does not state that the file cannot be accessed, just that the class cannot be loaded.
So if anyone still struggles, check the permissions. Some might find it obvious tho'.
I want to give the answer for the folowing link question ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse
Ans: In Myeclipse go to Server-->left click on Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)-->ok

Red.xml - It asks me to choose associated class files, When adding jar file by clicking Add Java Library

I'm having a situation to add associated class files of jar that I have chosen rather than adding the jar itself.
Steps Followed:
Deleted the existing Jar files from RED.xml.
Clicked 'Add Java Library' and chosen the newly created Jar file of my java project into RED.xml.
A window opened with all the class files that are associated in the jar file, having asked as "Select the class(es) that defines the
library". My Question is why? Am i doing something wrong? It wasn't
asked like this before when I used to follow the same steps.
Choosing all the class files displayed in the jar files window. Would the path of these files to be specified in the Robot.suite.
You're not doing anything wrong, as this is expected behavior. When you add a single class jar file, then it's easy to assume that this class will be the Robot Library. When you move to a multi-class jar file, that assumption can no longer be made. In order to determine which is the library and which are other (dependency) classes, you have to choose.

How to make boxfuse create an image containing executable jar and configuration file to be specified in main arg?

I'm playing around with boxfuse attempting to "fuse" an image which contains an executable JAR. My executable JAR is given the path to a config file as an argument to it's main method, like so:
java -jar my-executable.jar -conf /some/path/to/my/conf.json
Where the file conf.json is read in by the JAR's process to be configured with e.g. port, database connection properties, etc.
I understand how to pass custom arguments using -jvm.main.args="-conf /some/path/to/my/conf.json", however, I don't know how to get the config file into the image itself. Obviously the path has to point to a valid file that exists within the image.
In dev, test and production, I would want to use the same executable JAR, but a different config file for each environment. I don't currently see a way around having different images for each environment. I see there is some support for packaging specific config with Dropwizard payloads, but no mention of something similar for executable JARs.
Is there a more general way I can package arbitrary files into the image, with predictable paths I can refer to in the jvm.main.args?
P.S. in my case the executable JAR happens to be a Vert.x application, but I think the general case applies.
What you can do is package the configuration for all environments (dev, test & production) within the executable JAR file. So you would have dev.json, test.json and production.json
You can then use a technique like environment detection with for example an environment variable to detect the correct environment at runtime and pick the correct configuration, which can then be loaded from the classpath instead of the file system.
This allows you to build both the jar file and the Boxfuse image only once and run it unchanged on all environments.
P.S.: I've just raised an issue for you to add first class Vert.x support in the near future to make things even easier: https://github.com/cloudcaptainsh/cloudcaptain/issues/28

Being able to run java application on different computers without JDK

How can I send my java application to a friend without having to send the entire project and being dependent on him having JDK? I'm aware of the .jar-file's existence, but I don't know how to proceed. I would like to be able to just send him the .jar-file or an executable file.
Any ideas?
Compile it to native code using a compiler such as http://en.wikipedia.org/wiki/GNU_Compiler_for_Java
Also, he doesn't need the JDK, just the JRE.
Did what #Barranka said regarding the dist folder, didn't know that worked until I read the readme file as #Barranka suggested.
So to quote what #Barranka said:
If you work with NetBeans, when you "build" your project, a dist
folder is generated, and your "packed" app is stored there. You can
send the contents of this folder. Read the "readme" file in that
folder. – Barranka
You can send the JAR file alone provided your program does not depend on other libraries. Assuming that there is a main() method and Main Class is configured in the JAR's manifest, the person can run it by Double Clicking (on windows) or use the command line
java -jar <jar_file_name>.jar
You cannot execute a Java application without a Java Virtual Machine, so you need one.
Your friend has to download a JRE/JDK, or you can provide it with your application directly as it (the JRE and your project in an archive) or provide it as a native compile code using GCJ or Excelsior Jet which will compile your application and a JRE.
As you can see the is no solution for your question, but there is one for your problem : ask your friend to download a JRE.

Resources