I am having problems in running my Java program - java

I'm a beginner in Java so this question might seem a little stupid, my JDK and JRE are installed in C:\program files. I write my program and save it in in my folder, G:\start.
Now my program compiles without any error (the .class file is also generated), but when I run my program it says "unable to locate G:\lib\amd64\jvm.cfg", so I copy the 'lib' folder from my JDK folder and paste it in G:. Again program compiles without any error, but when I run it, it gives me a new error saying "unable to locate G:\bin\server\jvm.dll", so I copy the 'bin' folder to g:, but now when I run, it gives me the same error again "unable to locate G:\bin\server\jvm.dll". Where am I going wrong?

Create an environment variable called JAVA_HOME and point it to the bin directory of your Java installation. Also add the same directory to your PATH variable. This should solve your problem.
PS: JAVA_HOME may not be required, but it will help if you have tomcat and stuff installed.

try to save it as a .jar, go to your cmd and use:
java -jar G:\bin\server\jvm.jar
this should work.

The best advise I can give you is to download an IDE to get started. BlueJ is an IDE aimed at getting people started using java and is used in many universities world wide.
take a look at www.bluej.org and you should be up and running in seconds.
Karl

Related

Jar file executes in InteliJ IDE but not in windows

Running in intelij
When i try to run the .jar
Edit environment variable path
I've written my first small program in java, and want to make it into an executeable file.
I used InteliJ to generate a .Jar file out of the Main class. When i Run the Jar in Intelij it seems to work fine. However once i try to open it from my windows i get
error a jni error has occurred please check your installation and try again
Some googling told me that my JDK might not be properly set in my system properties, so i added the path to my JDK, however the error remains. Anyone have any idea what's up with this? I believe that I added my JDK through inteliJ. Why is it so difficult to simply run the program i made? Is there something wrong with my JDK? is my .jar corrupt?

Running a java project in terminal with multiple .java files (multiple attempts described inside)

I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.
The project is split over 4 .java files all in the same package.
My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).
I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:
javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java
This creates 4 corresponding .class files fine.
The Main is in bibliotecaApp.java/class, so I try run it by:
java BibliotecaApp
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)
Plus about 13 lines of specifics.
Now googling this error seems to be a class path problem, and this is where I get stuck.
From places I've read, usingecho $PATH gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is still from the directory of the .java files above.
That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.
That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:
java -jar biblioteca_jar
I get
Error: Invalid or corrupt jarfile biblioteca_jar
Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?
The code is on my GitHub if that helps anything
https://github.com/botagar/twu-biblioteca-JohnGeddes
Based on your compiler step, change this
java BibliotecaApp
to
java -cp . BibliotecaApp
Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.
A jar file is a kind of zip, and should have a .jar extension. So this
java -jar biblioteca_jar
should probably be
java -jar biblioteca.jar
And you can check if the file is valid with any zip archive reader. Including jar itself,
jar tvvf biblioteca.jar
Edit
Based on your comments below,
cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/
and then
java -cp . com.twu.biblioteca.BibliotecaApp

'Java' is not recognized as an internal or external command and program running

This is probably the most frequent question you get in the world, and I apologize, but I have to ask anyway. I recently downloaded the newest version of java (1.7.0_45-b18), and I recently finished making a small program for a local community of mine in Eclipse. I'd like to share it with them so anyone can run it by clicking it, but opening the jar file just hasn't seemed to work for me.
I attempted opening it with command prompt by using
java -jar StatCalc.jar
but it always tells me
'java' is not recognized as an internal or external command, operable program or batch file.
I looked up many solutions for this, the most common being to change your Path. So I went to the environment variables, and changed the path to
C:\Program Files (x86)\Java\jre7\bin\java.exe
But it still gives me the same result. Only when the directory is exactly in java, and the jar file is in the java bin folder can I run the program. I wan't any person to be able to run this.
How do I get command prompt to work and allow this program to be run by both me and other people in this small community?
The path shouldn't contain the executable itself - just the directory containing java.exe. So you want this on your path:
C:\Program Files (x86)\Java\jre7\bin
Restart your console, check that the path is correct (just run path and look at the output) and all should be well.
Note that if you're going to be developing Java code, you should probably use the JDK path instead of the JRE path. For example, my path contains c:\Program Files\Java\jdk1.7.0_17\bin. (Yes, I need to update :)
I hope you do not have changed your PATH variable, only added (...);C:\Program Files (x86)\Java\jre7\bin\ (on UNIX systems, use a colon (:) as the separator instead) at the end. Otherwise, you will have many issues with all other Windows applications. Note that you must add the folder that contains java.exe, not the path to the executable itself.
Then, try to restart your Command-Line or even your Windows session.
Optional: You may set JAVA_HOME to specify JDK location. See this link.
Here you have a tutorial of oracle for setting the path
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
C:\Program Files (x86)\Java\jre7\bin --> This would be your PATH variable.

jnativehook build failed jni.h not found

I have been trying to build jnativehook from source. I am using windows 7. I went through all the stages described on the website. Including change the environment variables Currently I am getting a build failed from the command prompt.
I have pasted it below.
Im not sure where the jnativehook files where supposed to extraxted too, so I have tried building the file while it is in different locations including the root directory, the java directory and inside the java folders.
I checked if jni.h is anywhere in the jnativehook (jhook) files and it isnt but there is a folder called jni one step up.
There is jni.h file in the java directory but it doesnt say anywhere that I have to move and files out of java to jhook directory.
Does anyone know if it supposed to extracted to specific place?
Also does anyone know what this error means and how to solve it?
Any help appreciated
:
[apply] C:\jhook\src\native\common/NativeErrors.h:36:17: fatal error: jni.h
: No such file or directory
[apply] compilation terminated.
[apply] gcc.exe: Files\Java\jdk1.7.0_07\include: No such file or directory
[apply] gcc.exe: Files\Java\jdk1.7.0_07\include\win32: No such file or directory
Try to use other versions of jdk such as jdk1.6.0
Hope this helps.
Its because you installed java to a location with spaces in it. This shouldn't be a problem in version 1.2 or the trunk. If you need to compile 1.1, escape the space with a \ or two in the java.include property. You can set that in the build.properties file.

noClassdeffounderror with external jar when running code from a different location

I'm learning Java and was trying to test my code on another machine but am running to the above error. I've looked at youtube videos, read forums(and SO) but still can't get this to work.
I basically wrote some code on my mac using eclipse that referenced an external jar file. I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path". The code works fine on my laptop.
But when I try to run it from a linux command line, I get the above error. I am taking the code from eclipse and copying it to a file(first_try.java) and then run this commands:
CLASSPATH=./jedis-2.0.0.jar;export CLASSPATH
javac first_try.java
java -classpath . first_try
but then I get the error: Exception in thread "main" java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis I downloaded the program via wget and checked permissions and tried different variations. There was a similar question I found(out of the many related ones) that had a similar context as mine but it worked for the user to just type java filename (which is not working for me)
I am learning a bit of java code but have never been successful at running external jars. I'm wondering what I'm doing wrong and what I can do to permanently fix it(ideally I want to write code locally and then copy it and test it on another machine like this)?
Or is there a better way to deploy code that depends on third party jars to other systems?
You are building your class path env var but then you don't use the value in your java command. You need $CLASSPATH instead of the dot after -classpath
Good luck learning java, I have enjoyed using it for over 10 years now ;)

Categories

Resources