How can I make .jar files to run on my computer? [duplicate] - java

This question already has answers here:
'Java' is not recognized as an internal or external command
(19 answers)
How to run a JAR file
(12 answers)
Closed 1 year ago.
I've searched for a while for a fix, I think I've tried all of the common fixes, I even tried Jarfix. I have JRE installed, and I deleted and reinstalled it again just to make sure, I'm running Windows 10 64 bit. If I double click the .jar just nothing happens, and I've made sure that .jar is set to open with the right file. I don't know what to try so if you have any suggestions, I'll try them, because I'm completely lost.
Whenever I try to run any command starting with "java" is gives me this: 'java' is not recognized as an internal or external command, operable program or batch file.

First of all, Add your JRE to the PATH of Windows.
When you add it there, running java commands in the command line, will be recognized since JRE will also be indexed.
This is how you add it:
set PATH=%PATH%;C:\your\path\to\jre\bin\here
An example:
set PATH=%PATH%;C:\Program Files\Java\jre1.8.0_271\bin
Then go to the folder where your jar file is located. And open a command line there.
Or go there through the cd command (which means change directory to jump to exactly this directory). Example:
cd C:\Program Files\Java\jre1.8.0_271\bin
And then run your jar this way:
java -jar yourJarName.jar

.jar files have the java directory directly inside of them so you shouldn't have to reinstall JRE.
Maybe your .jar file you are trying to run isn't put together properly.
OR there is nothing to run on your file or there is an error in the code.
Those are all ways it could be broken.

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

Java program with referenced libraries: NoClassDefFound [duplicate]

This question already has answers here:
NoClassDefFound when trying to run java with external libraries
(4 answers)
Closed 7 years ago.
How do I run a program in Java, with several referenced libraries, .jar files, from a command line?
I have put all my .jars in /lib folder, which is in the root folder of my project, and added those .jars to the build path.
So my project now looks something like this:
Project:
-->/src/Entry.java, ... (all them .java files)
-->/bin/Entry.class, ... (all them other .class files)
-->/lib/commons-codec-1.10./(all them .jars)*
Now when i try to run the program from the cmd:
I locate myself within the /bin folder and execute java Entry, but I get NoClassDefFound exception
How should I run this?
you need to tell JVM where to look for classes while running the program.
the parameter that we use to tell jvm that is known as classpath
there are different ways to achieve that
Recomended Add the classpath location to the run command , alternatively pass the jar locations, assuming you have two jar files a.jar and b.jar under you lib folder, the command should be java -cp".;lib/a.jar;lib/b.jar" Entry
Either put the jar files into a location that is already under classpath (Since current folder is always under classpath, easiest option would be to put the jar under current folder , but this is not a recomended way to achieve)
Modify you classpath variable under environment properties to list the folder containing your jar , which is a trivial way for achieving this.
Create one batch file (.bat) and keep the jars inside that. whenever you want to run then directly run that batch file. I guess its very simple and efficient.
Example:
#echo off
SET PATH=%PATH%;E:\Java\jdk1.6.0_45\bin // JDK path
SET LIB=%cd%\lib
set CP=""
set CP=%CP%;%LIB%\antlr-2.7.6
set CP=%CP%;%LIB%\commons-codec-1.8.jar
set CP=%CP%;%LIB%\opencsv-2.3.jar
javac -classpath %cp% *.java
java -classpath %CP% -Xms256m -Xmx1024m -Xss2m T2DPreProcessing
pause
I hope it will help you. Thanks.

Why i don't have .jar?

When download a file like "example.jar" the file isn't .jar is just a white file.
I tried finding an other .jar file from my java folder and Run As.. it but it just runned cmd for 1 second and then closed it.
Can anyone tell me what to do?
You have to run it via your cmd with command:
java -jar NameOfFile.jar
And you do not see the file extensions, because you most likely told windows in folder options to hide them.

How to make a .jar file run without cmd [duplicate]

This question already has answers here:
How to run .jar file by double click on Windows 7 64-bit?
(18 answers)
Closed 9 years ago.
Sorry if this a little dumb, but I have a .jar file that runs great when i type java -jar quiz.jar. But it doesn't work when i just double click on the file. I have it set to open with java.exe, and all my other .jars work fine. it's just the ones i've created. Any Ideas? Thanks.
What you can do is adding an option in your context menu (using registry).
For example, you can copy this code into a .reg file and run it.
You should have a "Run Jar" option in your context menu. I did it on my Windows installation and it works great.
[HKEY_CLASSES_ROOT\*\shell\Run Jar]
[HKEY_CLASSES_ROOT\*\shell\Run Jar\command]
#="java.exe -jar %1"
If want to remove this option, create and execute a new .reg file which contains :
[-HKEY_CLASSES_ROOT\*\shell\Run Jar]
Simply by doing:
right-click on the JAR,
"Open With...",
and then select the javaw.exe from your JRE's bin folder,
tick the "always use the selected program to open this kind of file".
Note that it has to be javaw.exe, not java.exe. Do browse to the correct location yourself to make sure.
This alternative should work as well.

"Could not find main class" on .jar execution [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running Jar file in Windows
I created a .jar file for a small GUI Java project with NetBeans. It runs fine from command line. I have .jar files associated with javaw.exe in the JRE. But when I double click the .jar file I get an error:
Could not find main class.
The fact that double clicking is saying that it cannot find the main class means that a JRE is installed. That is NOT the problem. It also means that a suitable shortcut exists. That is NOT the problem ... either.
The problem is (I think) that the JAR file has not been correctly created as an executable JAR file. An executable JAR file must have a Main-class entry in its manifest that tells java.exe or javaw.exe which class contains the "public static void main(String[])" method that is used to start the application. Either the manifest entry is missing, or it refers to a non-existent class.
Since this is your program, you need to take a closer look at the way that you are creating the JAR file. You most likely need to tell NetBeans which class to use as the entry point / "main" class.
check this answer: Running JAR file on Windows.
In all likelihood, you'll need to supply the javaw.exe" -jar "%1" % part in a shortcut, and that will fix the problem.

Categories