Can't Run JAR Files - java

I can run my project through netbeans but after I make a jar file and double click it I get the error cannot find Java runtime environment? I am on Windows.

You don't have a file associate created. Right click on the .jar file in Internet Explorer, select Open With, and navigate to your JRE. Select the java.exe executable, and make sure the command-line argument has ' -jar ' present.
The .jar file will be passed as the first argument to the JVM.

Make sure you have JRE installed.
Open the console and type java -version

You should download a Java runtime environment.

You might try, as Chris said, making sure the JRE executable is part of your PATH environment variable. Check out "4. Update the PATH variable (Optional)" from this guide.

Make sure the JRE bin dir is in your PATH env variable.

It's all about the Manifest - http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html

In Netbeans run the project. It will ask you what class to run (you will see a list of all the classes with a "public static void main(String[])" method). Once you do that the next time you build the class netbeans will show you how to run it (with the -jar witch). Now you should be able to double click on it.

Related

Unable to run JAR file outside Intellij

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

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.

Javac isn't working in windows command prompt

javac not working in windows command prompt
^I tried the suggestions on this post, and it was SLIGHTLY helpful, but not completely.
I opened up my command prompt and I typed in "javac" after putting in the path in my Environment Variables and it didn't work, at which point I googled it and found that thread.
I knew that I had closed and re-opened my cmd already, and that didn't work, so I skipped that bit and I saw the part telling me to make sure that javac.exe exists, which I verified with the "dir" command in the cmd. Afterwords, while in the "C:\Program Files\Java\jdk1.7.0_25\bin" folder on the command prompt, I typed in the next bit of advice, which was
for %i in (javac.exe) do #echo %~$PATH:i
After entering this into my command prompt, I got the message "ECHO is on". Upon seeing this, I typed in "javac" again and this time, it worked. So I decided to test this out by backing out of the directory and going to a folder in which I had a .java file saved and running it, but it again told me that
'javac' is not recognized as an internal or external command, operable program or batch file.
This was disappointing. I think it'll only work if I'm INSIDE the bin file on the command prompt, which is annoying because I'm not an administrator on this computer and it will be annoying to always have to get admin permission (from my parents) to code. They will also never give me the password. Can anyone help me? Thanks in advance! And sorry for the huge wall of text...
EDIT: Someone has asked what would the output of "echo %path%" be. It is this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
^Is it not supposed to say this?
EDIT 2: #brano88...I think I did? I right clicked computer, went to properties, went to advanced system settings, clicked Environment Variables and went to the top part and pressed "New..." There, I entered the Variable Name as Path and the Variable Value as the location of the bin folder. Is this incorrect? I followed a YouTube tutorial step by step while doing this.
This one: http://www.youtube.com/watch?v=Hl-zzrqQoSE
How to run .java files from CMD
go to your Computer -> C: -> Program Files -> Java -> jdk1.7.0_25-bin
copy the path (example: C:\Program Files (x86)\Java\jdk1.7.0_25\bin)
Go to Control Panel -> System and Security -> System-Advance System
Settings -> Advanced -> Environment Variables
open the Environment Variables screen and go to System Variables and look for "Path"
after finding the Path system variable, double click it or press edit button and in the Variable value you paste the path from java you just copied after the last values already existing there.
Note!
make sure you DO NOT enter any extra space in this field as it won't work;
make sure you have one semi-colon before pasting the path, example: Path :
...%ANT_HOME%\bin;C:\Program Files (x86)\Java\jdk1.7.0_25\bin
Note! If you previously tried to compile the .java file in a CMD, close that CMD
and open it again as the changes made will take effect only using a new instance of CMD
Go to the location of the file.java , example:E:\Projects , right-click by holding the Shift button pressed inside your folder and in the options from the window that just appeared select: Open command window here
Another solution is to normally open a CMD and change the directory using :
cd command until you reach your folder
After the CMD window opened, type: javac HelloWorld.java
Note! Make sure the class name written in your file.java is the same as the file name. Example your file name should be: HelloWorld.java and your class inside that file must also be:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Otherwise, it will not compile!
It is successful if no error message is displayed, if the path of the folder appears again in CMD and of course if the HelloWorld.class file appears now in the directory. To check that type the command dir which will show what contains your currect directory.
To finally run the file type in the CMD: java HelloWorld
Observe that no extension is needed when running the file(the file you run already has the .class extension)
This is how it worked for me! If something is not right, please inform me! Thanks!
For a beginner programmer that doesn't have admin rights on his/her computer, I'd recommend the Eclipse IDE.
Since you already have the JDK the only installation step needed requires no admin rights. From here you must download the "Eclipse Standard" option, and you will get a very large zip archive. You can extract it onto the desktop or my documents. Windows comes with a utility to do this via drag-and-drop right from the explorer or your machine may have another program such as WinRAR installed to do this.
You can then run eclipse.exe from the place where you extracted it by browsing to, and double-clicking this file.
The IDE is very powerful and self-explanatory. You can create projects, run, and debug code, and it's nice for beginners. It's truly worth the long wait in downloading it.
First, yes you did add JDK to PATH. But you didn't do that correctly. You already have these variables added to PATH: C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
To add JDK put semicolon before you add it because you have multiple path's assigned to PATH variable. So it should be something like this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_25\bin
in my case, javac was not working just because while specifying the path to javac in the path variable I gave space after putting semicolon to the end of the previous entry
To use javac from command prompt without typing the full location each time you will need to add it to the path.
I am unsure if you can change it without admin privileges, But on windows 7 with admin you can go to start -> Right click My Computer -> Properties -> Advanced system settings -> Advanced -> Enviromental Variables
You can then find the system variable path and append to the end of it, the location of javac.
The exact location of javac will vary depending on what version of the JDK you have installed, During installation you will have been given the option to choose where it was installed.
EDIT: Also make sure you haven't opened cmd as administrator. Or perform the steps in that video on the administrator account.
Adding variables to the top part of the environmental variables menu, mean they only affect the current user. You should be able to do these on normal account if you have someone type in the administrator password.

Starting with Java

So I am trying to start with Java (as in, trying to get the dang thing to accept code). I download all the needed things (the SDK) from Java and such, but when it gets to the point where I have to do "javac" in Command Prompt to compile the notepad file, I just get the message saying that there is no command called "javac".
Anybody wanna share some insight?
To set the environment variable PATH: http://www.java.com/en/download/help/path.xml.
Also I recommend using an IDE such as netbeans or eclipse. They make it much easier when starting off in java, plus when getting into advanced projects with many classes, they help greatly.
You need to set the path to your java compiler for it to be found when you use the command prompt. This page explains how.
I would suggest setting the following environment variable:,
JAVA_HOME to point to the root of your java installation e.g. C:\Program Files\Java
than append the following to your PATH environment variable:
;%JAVA_HOME%\bin
than you will be able to use java and javac from the command line.
Also see this article from Microsoft on setting environment variables if your not familiar with it.
You need to add the Java bin directory (where javac.exe is located, assuming you're on Windows), to your system PATH.
Right click on "My Computer", go to Environment Variables, and add the bin directory where Java is installed to your PATH variable.
You will need to have the java bin directory on your path. So, on windows, if installed at c:\java, and bin is c:\java\bin (normally you have version number, jre vs sdk, etc), you will need to add that to your PATH environment variable. set PATH=c:\java\bin;%PATH% -- you could do this in a setlocal/endlocal block or set it permanatly for your machine.
Also, the JRE may not have javac -- you may need to dowload the SDK.
Starting with java by typing 'javac' at the command line satisfy my mother's definition of the phrase 'starting with' ... as in,
"Don't start with me, buddy."
You are likely to end up with a punch in the nose.
For your own sanity, pick one of Eclipse, or NetBeans, or IntelliJ, or the other popular IDEs, and start from there.

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Categories