New to Java but I am having Issues - java

I have been doing research on why Java keeps closing immediately upon opening it. I am on Windows 10 and running on a 64 bit. I did install the correct Java download for Windows 64 bit. I have uninstalled it and installed it about four times but nothing seemed to help. I also double checked to see if it was up to date and it was at the latest version. I am unsure why this problem is occurring.
Any advice?

You should not directly run java.exe from the jdk bin path. You must do it in command prompt. The main thing is if you are not using any IDE and trying to write your code in any editor then manually run it. then you have to set java path as environment variable.
Watch this video to do that https://www.youtube.com/watch?v=l1VxWWubxFo .
Then write a simple java program , save it with .java extension.
Then open cmd there where you have written the program by holding shift key and right click on the file explorer, you will see a option like open command window here.
Then write javac <your java file name with .java extension>.
Then write java <your class name in the program>. Ignore these < > angle quotes

Related

JAVA : Program Works in Terminal , But not in Visual Studio Code : Mac OS

My JAVA Programs used to run without any errors on both the Terminal and Visual Studio Code's Terminal .
After my Mac updated to MacOs Big Sur Version 11.0.1 , my JAVA programs show an error , even though the same code worked properly before .
There was no error before.
I checked my JDK and JRE Installations also
My Python files execute normally in the VS Code environment , so I do not think there is a problem with VS Code .
I have tried searching for an solution and have tried to clear the logs.
I would appreciate if anyone could guide me :)
Run the .java file in Terminal to check if JDK can work normally:
javac Hey.java
java Hey
If there's nothing wrong with the above command execution, turn to VS Code, uninstall Java Extension Pack and also delete the related folders under User/name/.vscode/extensions, then reinstall it again;
Set java.home and java.configuration.runtimes in User Settings.json.
Reference: Configure JDK.
Then run the project again to see if the problem goes away.
Install Java by searching for extensions in VSCode and try to run again.It will be executed. I belive it is not installed
You need to install the java plugin in VS Code.
Hi just found a solution here!
Initially guess it should be a problem with zsh.
so first open your terminal and type
open ~/.zshrc
at this point you will see text editor pops up with a zsh config file.
Add
export JAVA_HOME=`/usr/libexec/java_home
to the last line in that file, and save the file quit.
Then just reopen the VSC and you will notice everything would back to normal as usual.
I faced a similar problem. When running the same Java program, it worked in terminal but VS Code couldn't detect java.
All it turned out to be was I forgot to add Code itself to Path after reinstallation, nothing to do with Java.

Javac working with Powershell but not Command Prompt

I am trying to install Java in Windows 10 Home (64 bit). Like many tutorials point out, I downloaded the Java jdk setup file, executed it and once installed changed the Path in the Environment Variables. But for some reason, when I execute the command 'javac' it returns the not internal/external command error in Command Prompt but runs absolutely fine in Powershell. I tried to rectify this by uninstalling Java and reinstalling it very carefully, following all the steps but still this keeps on happening.
Also, during installation of Java, I lost the original value to my Path variable. Could this be causing the problem?
Edit : I was able to get the java command running in the command prompt but javac still won't work. I've already tried the methods suggested on other forums and this one but no luck so far.
See there is no problem with Java installation if you can find JDK(Java development kit) and JRE(Server Java runtime environment) in programme files in windows in a JAVA folder.
The main problem is with the environment variable declaration.
Go to environment variables and make a new path for that.
JAVA_HOME = "YOUR JDK & JRE INSTALLATION PATH"
And another one is
JAVA = "YOUR JDK & JRE INSTALLATION PATH"
Hope that help to you. After doing that if still there problem persisist just restart your PC and check again.
Just add your variable location up to /bin/
Refer to this :
https://www.windows-commandline.com/javac-not-recognized-internal-external-command/
Open file explorer
Right click computer then click properties
click advanced system settings
click environmental variables
click path (for me there was one in user variables for admin and another in system variables)
find the directory for javac (for me it was here C:\Program Files (x86)\Java\jdk1.8.0_191\bin)
so go back to file explorer and open program files x86
then open the java folder
then open the jdk folder
then open the bin folder
highlight the path in the search bar and copy it
click edit on path and paste into the path in the environmental variable for both(if you have more than one path)
If no path exists then create one in user variables
If there is any existing text place a semicolon ; between the text you pasted in there and the preexisting text.
close command prompt and reopen it.
type javac -version
the version info should be listed
This problem comes up with the directory of your command prompt.
check your PowerShell and command prompt default directory.
We can run command prompt as WinKey+R then type cmd, Hit the enter button then check our java version cmd javac -version.

Trouble in Setting Java Environment Path on Ubuntu (libjvm.so: cannot open shared object file: No such file or directory)

Found the solution!
Ubuntu by default ignore the command "RD_LIBRARY_PATH" therefore I need to add those codes in ".bashrc" instead of "/etc/environment" or .profile.
it now work perfectly fine!
I am trying to run a program that reads Java method from C/C++ code. (I made it on Windows successfully, now, I am working on Linux)
I made it successfully compiled and run on Ubuntu, however, the problem is..
Whenever I try to run the program, I have to type following commands on terminal..
source /etc/environment
./invoke #program name is invoke
If I do not type "source /etc/environment" every time I boot my PC, the error message I get is...
error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory
Ok. Here is what I added in /etc/environment
export PATH="$PATH:/usr/lib/jvm/jdk1.7.0_25_x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_25_x64
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64:$JAVA_HOME/jre/lib/amd64/server
What I want is that, as you know, I want to run the program without typing "source /etc/environment".
I also tried typing those PATHs on /etc/profile, but result was same... I should type "source /etc/profile" every time I reboot my PC..
How to make it permanently applied to whole range of System without typing "source /etc/environment" stuffs everytime I boot?
if you need to see source code of the program, here is downloadable address:
http://cfile237.uf.daum.net/attach/247819495212DF1C07B9EB
This worked for me:
For x86:
export LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/i386:/usr/lib/jvm/default-java/jre/lib/i386/server
For x86_64
export LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/amd64:/usr/lib/jvm/default-java/jre/lib/amd64/server
I had the same problem of setting the path(permanently) however I was using Red Hat, If you look change PATH permanently on Ubuntu it might help you. There was a file on Red Hat .profile that once you add your exports statements, they will get loaded when the computer starts. :) There should be the kind of file for ubuntu
export PATH="$PATH:/usr/lib/jvm/jdk1.7.0_25_x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_25_x64
Put "-Djava.library.path=FULL_PATH_HERE/jre/lib/amd64" as a JVM option in your C code.
Edit the full_path_here part.
The path is allowed to be relative, but I don't think you can use $JAVA_HOME as a placeholder for the environment variable.
Found the solution!
Ubuntu by default ignore the command "RD_LIBRARY_PATH" therefore I need to add those codes in ".bashrc" instead of "/etc/environment" or .profile.
it now work perfectly fine!
add the path of the missing libraries in the file /etc/ld.so.conf
execute the following command: ldconfig
this way, you'll add the new libraries to the default ones.
To check whether the new libraries have been linked,
execute the following command: ldconfig -p
to have the list of libraries currently linked by default.

java can run jar from cmd but not by double clicking

I just created a jar file using jdk7. I tried running it but kept getting an error. It turned out it was pointing at a jre6 and not the jre in jdk7. So, I used the following command to change it
ftype jarfile = "C:\path to jre\bin\javaw.exe" -jar "%1" %*
After that, I tried to double click on my jar file again, but it popped up a window asking me to choose a program to open it with. When I chose the same javaw.exe file as above it runs but exits immediately without doing anything. I can run it fine in cmd by doing the java -jar file.jar command. I even tried deleting the .jar registry key, but that just got me back to the point where it asked me to choose a program to open with. I'm running windows 7 ultimate if that matters.
EDIT:
In the registry there are 3 subkeys: OpenWithList, OpenWithProgids, and UserChoice. I've changed all the defaults to the path above except UserChoice which won't let me edit it from regedit. When I delete that and try to run the jar file, it opens up the choose a program to open with window again. For some reason it ignores the other registry keys
OK, I found the answer somewhere else but ran into this issue again so came here via google.
The solution is...
Open up regedit.exe
Either search for "jre6" or follow this: HKLM->SOFTWARE->Classes->jarfile->shell->open->command
Change the Data field to the directory of the javaw.exe file that's in your JDK directory.
For the love of God, don't forget to put the weird [-jar "%1" %*] parts on the end of it.
Enjoy outsmarting Oracle.
Hopefully this'll save some people a bit of time.
This happened to me when I installed the JRE6 plugin for Chrome, though it could happen when installing any other JRE I guess.
I fixed it by going to regedit and searched for jre which brought up...
HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command
I changed the REG_SZ
from: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" "%1"
to: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
When windows asks you for a program to run it against it won't insert the needed -jar argument in the command line.
If it's asking you for the exe in which to run it then that means somehow your JRE isn't associated with the .jar extension. You'll need to modify the .jar file registry entry so that it runs c:\path_to_jre\bin\javaw -jar %1 in order for it to work.
It used to be under File Explorer > Tools > Folder Options > File Types.
Then look for .jar in there. You should find a text field that shows the executable to run and the command line arguments. That's what you want to modify. You can also do it in the registry editor too, but I've forgotten the HKEY variable path. I'm sure this KBA will help:
support_microsoft_kb_950505
Check if the file type jarfile is actually registered with the *.jar extension using
assoc .jar
Seems like .jar is not linked to jarfile.
I'm not sure exactly why none of the solutions worked, but I reinstalled jre7 and it works now.
Isn't it easier to change your java version to you rnew jdk7 by changing the CLASSPATH environment variable? See how here

Windows Registery: How to add a Java app to startup list?

I added a value at:
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
That looks like this:
Value Name: LDE
Value Data: "java -jar C:\LDE\lde.jar"
Really with the quotes (Because all the others where also with quotes). After adding this, I restarted my computer, but it didn't start automatically.
Will wrapping my jar in an exe help?
I'm running Windows 7.
Any help?
Thanks in advance.
Update:
When I remove the quotes, it works. But now there is appears also a terminal, which I don't need...
A couple of things to note here, concerning the two different issues in the problem:
Format of Windows Run keys
From the Microsoft Windows XP knowledge base:
Run keys cause programs to
automatically run each time that a
user logs on. The Windows XP registry
includes the following four Run keys:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
Each of these keys has a series of
values. The values allow multiple
entries to exist without overwriting
one another. The data value for a
value is a command line.
Note the emphasis on the last line. The moment quotes are used, the command is bound to fail execution in the same manner it fails as if executed from a command prompt.
Also, note that the above approach is for Windows XP and does hold good for Windows 7. More details can be found in this Microsoft Technet article on the options available in Windows 7.
The javaw vs java application launcher
Once the java process can be initialized at Windows startup, one will get a console window that continues to stay around until the process is terminated. This occurs if the java executable is utilized to initialize the application.
From the technotes of the java application launcher:
The javaw command is identical to
java, except that with javaw there is
no associated console window. Use
javaw when you don't want a command
prompt window to appear. The javaw
launcher will, however, display a
dialog box with error information if a
launch fails for some reason.
Therefore, if you wish to avoid opening a console window for the Java process, you ought to use the javaw executable.
This is very simple. You will find the startup folder in the C:/Documents and Settings/AllUsers/YourUserName/StartUp. It will be on similar kind of path just check it. Then just paste your jar file in that folder and it will work nice. Remember that you put the jar file in the startup folder of your user name folder. You may find that this folders might be hidden so just check it out. If you find this answer useful vote it. Enjoy.....

Categories