Unable to run JAR file outside Intellij - java

I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?

Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>

Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).

After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version

Related

Where should vecmath.jar go in MacOS(11.5.1) [duplicate]

This question already has answers here:
Import javax.vecmath
(6 answers)
Closed 1 year ago.
Preamble: So this all started with just trying to use javax.vecmath.Vector2d. I didn't have javax.vecmath so I spent a bit of time trying to get it, found that I needed to download Java3D.
After a long time of trying to download Java3D for Java (version 16.0.2), I eventually got it together with the vecmath.jar file landing in /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/lib/ext. This got rid of the error: package javax.vecmath does not exist error message.
Then, I got the message
<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.
.Error: Could not create the Java Virtual Machine.
this also wasn't letting me use any java commands in shell.
A bit of research and I concluded the solution to be moving (via Finder select and drag) j3dutils.jar, vecmath.jar, and j3dcore.jar over to lib and just deleting the lib/ext directory. I have gotten rid of the <JAVA_HOME>/lib/ext exists problem but back to error: package javax.vecmath does not exist.
I don't even know what to do know. I just want to use javax.vecmath. Am I going about this the totally wrong way? How can I get this to work?
Okay, I figured it out.
How to use javax.vecmath in Mac OS(11.5.1) with Java(16.0.2)
I am giving a description that sort of includes why I do things, skip to the TLDR at the bottom if you just want an answer.
Step 1: Download the latest version of Java3D
This contains vecmath, along with j3dcore and j3dutils. It will download a .zip file. Unzip the file and it will expand into a new directory with another .zip file inside, j3d-jre.zip. Unzip j3d-jre.zip and it will expand into a directory lib. Inside lib will be a subdirectory, ext, with three .jar files inside: j3dcore.jar, j3dutils.jar, and vecmath.jar. You can put these literally anywhere, just make sure you keep track of their location (I put them in ~/Library/Java/Extensions, this location is on the hard drive and will need an admin password to do anything–use
sudo unzip /path/to/j3d-jre.zip
if you are doing things in shell). You CAN put the ext directory in JAVA_HOME/lib/ but after Java 6, this will cause a problem.
Step 2: Change CLASSPATH
Java has no idea how to find vecmath.jar so you have to specify it.
Option 1: Specify CLASSPATH with every shell command
The simplest version is using
javac -cp ".:/path/to/vecmath.jar:" MyMainProgram.java
to compile and
java -cp ".:/path/to/vecmath.jar:" MyMainProgram
to run the program (you can also replace -cp with -classpath and it will do the same thing)
This option won't ever destroy your CLASSPATH but you also have to include the -cp command every time you compile and run a program that imports javax.vecmath.
Option 2: Specify CLASSPATH with every new terminal window
A little more lasting than -cp, you can define CLASSPATH such that any changes will only take place in that terminal window. Use this form:
export CLASSPATH=".:/path/to/vecmath.jar:"
Now when you call
javac MyMainProgram.java
java MyMainProgram
Java will see that CLASSPATH is .:/path/to/vecmath.jar and everything will compile and run without adding the -cp command.
The main downside of this option is that if you update CLASSPATH again, you have to remember to add the previous CLASSPATH (which you can see at any time with echo $CLASSPATH)
Option 3: Permanently add CLASSPATH to terminal
Enter the following into terminal:
open ~/.bash_profile
this will open a window that may or may not have code in it. Regardless of any pre-existing code, scroll to the bottom and add
export CLASSPATH=".:/path/to/vecmath.jar:"
This option holds the CLASSPATH in all terminal windows forever or until you change it (using any method above).
TLDR
Download Java3D for macOS
Unzip java3d-1_5_1-macosx.zip and open the directory it creates
Unzip j3d-jre.zip and open the new directory /lib/ and the subdirectory /lib/ext/
Move vecmath.jar, j3dcore.jar, and j3dmath.jar to ~/Library/Java/Extensions (this requires an admin password) or any other location
Run the following line in terminal:
open ~/.bash_profile
Go to the bottom and add the following:
export CLASSPATH="/path/to/vecmath.jar:$CLASSPATH"
import javax.vecmath.* to any .java program you want
The jar file can go where you want, moving it to your project's lib folder is good. The real issue is you need your classpath to point to it.
Here is a full explanation.
If you are running from the command line you don't need to set the classpath variable, you can provide it in the java command. It would be something like this:
java -cp lib/vecmath.jar Example
This assumes that the program you are working on has been compiled into a class file named Example.class. If you main method is in a package you will need to fully qualify the classname so it might look like:
java -cp lib/vecmath.jar com.demo.Example
You can list multiple jar files on the classpath, separated by a colon (:).
You can also ask for help in the command line by invoking:
java -h

Error when executing class file in command prompt

I'm currently in the process of learning more about Java. I downloaded a sample program that has about 5 different .java files in the source folder.
Whenever I try to execute the class files in the command prompt using "java ClassNameHere", I get a message in the cmd prompt that says "Error: Could not find or load main class ClassNameHere".
I've tried recompiling them using the javac command and they compiles without any issues. Its just the class file i'm having trouble with.
In addition to this, I also cannot run the java files using Eclipse. Instead of getting an option to "Run as Java Application", "non applicable" appears under the "Run as" tab in Eclipse.
I've tried a number of different solutions already posted online, including another command that uses "java -cp . ClassNameHere" or something along those lines and they have not worked for me. Any help is appreciated.
Edit
Link to program from my drive since I'm not home
https://docs.google.com/file/d/0B9_pcTVZTnfEeVdzZmM0ZmFkTmc/edit?usp=docslist_api
Here is a a procedure that will get the application working in Eclipse. This procedure assumes the following:
Windows 8. I don't think Linux/Mac OS X procedure would differ greatly, but just in case...
In Eclipse:
Right click in Package Explorer, and select New --> Java Project.
In the New Java Project dialog, uncheck Use default location, and click Browse and select the directory you've extracted the zip to, or type the path in the box.
Click Finish.
From this point, you can run this is a Java Application from Eclipse.
Confirm that at least one of the classes has a main() method. That would be required for Eclipse to show "Run as Java Application".
Follow this steps:
Run Command Prompt.
Use cd C:\path\to\your\porject\java classes.
Use set path=%path%;C:\Program Files\Java\jdk1.7.0_65\bin (CHANGE THE PATH TO YOUR jdk1.x.x_xx\bin FOLDER) to tell the system where to find jdk programs.
Use javac MainClass.java to compile the project, MainClass.java si the java class where the main method is.
Use java MainClass (WITHOUT .java EXTENSION) to run the project.
After the last command your project should run correctly.
Sorry for the "use" ripetition but i'm not english.

Java application will run from CMD and Eclipse but not double click

I've done a search and I can see that a lot of people have had the same problem as me, but none of the solutions have worked for me.
Basically I have a Java Project in Eclipse that is from my old Windows Installation. I've cleaned and rebuilt it because at first it wouldn't compile, but now I have it exported as a Runnable Jar. However, the only way I can get the application to appear is to do java -jar foo.jar in command prompt, or run it in Eclipse. If I double click the JAR in Windows Explorer nothing happens even though I know that Java is associated correctly because other Runnable Jars work.
The project only has the x86 JRE listed in it's Build Path Libraries and all the files listed appear to exist. I'm running Windows 7 HP.
Update: I'm sorry, but I just discovered that no other Runnable Jars are working either. If they are wrapped with launch4j they work though...
Edit: The Runnable Jars that I export from Eclipse do work fine on other systems and load on double click
Some registry values or file associations are probably messed up. Wiping off all of your existing JRE's and JDK's and re-installing them should fix your issue.
Alternatively you may be able to fix it by manually editing the registry value here:
HKLM > SOFTWARE > Classes > jarfile > shell > open > command
My value is
Type: REG_SZ
Data: "C:\Program Files\Java\jre8\bin\javaw.exe" -jar "%1" %*
You'd of course want that path to point to your javaw.exe, and make sure you have the additional arguments.
I was also facing the same problem while i was working with Spring tool suite.
You may use the following steps:-
Right click on project -> export -> Runnable jar file -> (Here,In library handling,there are three options,you have to choose middle one i.e package required library into generated jar.It will package external dependency also).
-In my case, my runnable jar was only executing on my environment i.e on which i have created that JAR. Initially i have selected the first option to create JAR i.e extract required libraries into required JAR.but that was not proper.
It may help you.Let me correct if i am getting wrong.
Since you are able to run the JAR running the command line, I believe your issue is related to which version of Java is set to run the file when it is double-clicked.
To find out which version is successfully running the file from the command line and set it to open JAR files by default:
Open a new command prompt window.
Run echo %path%. Among the path values, you should be able to find one pointing to the bin folder of one of the installed versions of Java. Copy this path somewhere.
Navigate to the JAR file you would like to run. Right click the JAR -> Open with -> Choose default program... -> Browse...
Browse to the path you copied in step 2. (the easiest way is to paste it into the address bar)
Double click javaw.exe.
Click OK.
You should now be able to run the JAR file. Please let me know if your problem persists.
I have made a jar with and keep it on desktop.Then,I double clicked on the jar and it working fine for me.
How I and what I have monintored: In my main class, perform some operation and at the end I add on Thread.sleep(25000); to hold the program for few moments. After every double click on the exported jar I found one new javaw.exe process added in the system process tree. I have noticed it on Task manager. and after 25000ms respective javaw.exe process ended. As my application does not cointain any GUI that's why I have not seen any GUI changes for the respective process. I'm Confident that if my application have some GUI, I will surely get the respective GUI window on every run.
Common mistakes : when we export a project from Eclipse as Runnable JAR file, it is exported with selected Eclipse launch configuration and Eclipse specific launch wrappers. Now if the configuration does not match when you are trying to run it via double-click you will not be able to see the error, If you run it from CMD then surely you will get the error log.
To create standard executable JAR file : To create a standard executable JAR file, you can export as JAR file and specify the main class in last screen of the wizard.
That's working for me fine. I used the same jar from different system and keeping it different location.

Launch Eclipse Program Exported From File

I already read many questions it the same subject, but none solved my problem..
I know that I can easly launch my app using this command on console java -jar myappname.jar
But what I want is to click on my .jar file exported by eclipse and it launches console with my app inside, do u understand?
I done the export using this configs :
File>Export>Jar File
Selected all the classes of my project
Selected "Export generated class files and resources"
Selected "Export java source files and resources"
Selected "Compress the contents of the Jar File"
Pressed Next
Selected "Export class files with compile errors"
Selected "Export class files with compile warnings"
Pressed Next
Selected "Generate the manifest file"
Selected Seal the Jar
And on "Select the class of the Application entry point:"
I choose my class where is the void main method .
the jar appears on my desktop, but then, when I double click it doesnt launch the console. why??
Thanks in advance!!
Launching a jar by double-clicking the file (or shortcut) will not display a terminal. One workaround is to change the default execute action in your operating system for .jar files to open a terminal and execute the command from within the terminal. A script like the following might do the trick (using Bash):
#!/bin/sh
/usr/bin/gnome-terminal -x java -jar $*
sleep 3
Then right-click on the jar file and choose the script as the default program to run for that file type.
Disclaimer: the above script actually fails for me. It works fine if the command being run in the terminal is "top", so it looks like you may need to tweak this a bit.
It depends on your OS.
If you use Windows, you can create .bat or .exe files. You can find 'how-to-create' tutorials on the internet.
If you use Unix based OS, you can just set the jar to be the executable.
A third party OS probaly has it's own way to set to executable.
This is the only way I am aware of.

Java - Eclipse packages

I have the following package on Eclipse:
com.mortgageapp.projects.app
I'm not interested about the package format at the moment, it's just testing. But I'm wondering how to run the app from the terminal (Windows and Mac)?
It contains a Main.java file where it will begin so I have tried locating and entering the src folder. Then doing something like: javac com/mortgageapp/projects/app/Main.java (or: javac com/mortgageapp/projects/app/*.java).
Just wondering if this is current as when I then do: java com/mortgageapp/projects/app/Main I get a few errors.
Your compilation is probably okay, but to run it you need to specify the class name, not a filename:
java com.mortgageapp.projects.app.Main
That's assuming the current directory is in the classpath. If it's not, you may need:
java -cp . com.mortgageapp.projects.app.Main
I assume you are writing an RCP application.
Export the project as a RCP application (click the "export" link in your product configuration). You should get a runnable application file.
If you really want to run the application from the terminal, you need to use the main function in the EclipseStarter. But you don't want to go there unless you are doing something very special.

Categories