Sorry I couldn't be more specific with the question itself. Here's what I'm running: Windows 7 64bit, JDK v7, JRE v7 (64 bit), and JRE v6 (32 bit). I'm pretty new when it comes to Java, however I do know how to compile a program and all that stuff. I kept getting an error when I tried running my compiled programs:
C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1>javac ch3p1.java
C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1>java ch3p1
Error: could not open `C:\Program Files (x86)\Java\jre6\lib\i386\jvm.c
C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1>java -cp ch3p1
Error: could not open `C:\Program Files (x86)\Java\jre6\lib\i386\jvm.c
C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1>java cd3p1
Exception in thread "main" java.lang.NoClassDefFoundError: cd3p1
Caused by: java.lang.ClassNotFoundException: cd3p1
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: cd3p1. Program will exit.
C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1>java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)
note that it couldn't find the file because I had changed the folder name to see if it would automatically detect the 64 bit JRE I have installed
However, after enough searching I found a page that stated my problems were because I was running a program I compiled on JDK v7 in JREv6. I messed around with that for a little while, trying to get it to run in the JRE that came with my download of JDK.
CD: C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1
Current directory: C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1
javac ch3p1.java
Process started >>>
<<< Process finished.
C:\Program Files\Java\jre7\bin\java.exe ch3p1
Process started >>>
Error: Could not find or load main class ch3p1
<<< Process finished.
I'm using Notepad++ and NppExec to compile faster, and I gave up trying to get it to run JRE v7 and just used the location of it. And here's my NppExec code (I don't know if it's important, but I'll post it anyways):
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
C:\Program Files\Java\jre7\bin\java.exe $(NAME_PART)
And, finally, here's the actual program I'm trying to get to run:
public class ch3p1
{
public static void main(String args[])
{
System.out.print("Hello.");
}
}
So, a quick recap. The first error I found out was because I was compiling and running a program with different versions of JDK and JRE. I couldn't figure out how to actually make it run with the JRE that came with my JDK installation so I used the direct path to it instead. My hopes got crushed that this would work when I went to run my compiled program. I got a simple "Error: Could not find or load main class ch3p1".
What's the question? I don't know exactly, the broad question is: What do I need to do to get this to work? I cannot be any more descriptive than that because I have no idea what to do or where to look next. I'm hoping I provided enough useful information and structured it well enough to make it comprehensible.
Some other questions: Will removing the two Java 6 updates in my Add/Remove Programs work? How do I change it so whenever it runs java, it'll run the 64bit JRE v7?
I have no idea what I'm doing here and I'm basically learning as I go. I am familiar with computers to an extent but I am new to Java and JDK so I don't know much about how it works. My leads might be completely misdirected and wrong, if so, please correct me.
EDIT
I understand that there are a lot of questions on this website and I'm sure the people posting here answer a lot of questions that can be solved with minimal to no effort involved. I have to say that I have looked everywhere and I came here to post this because I simply cannot find the solution to my problem. I wasn't looking for help on finding another program to use, and I definitely was not putting the files in my Java installation directory because despite me being new to Java, I am not new to programming. That response could have been easily avoided by looking at the first example I provided, and another one where the file path to my .java and .class files can be seen in a folder in my Documents.
I will list answers and what I have tried here:
I reinstalled JDK and JRE from the Oracle website
I have set my SET_HOME system variable and my Path system variable has the JDK bin directory in it.
My .class file and my .java file are in the same directory, and my class and the .java file are both named the same thing, including exact cases.
After I installed JDK and JRE I deleted my previous files and made a new one, and that didn't work either.
I tried putting the direct locations of java and javac as suggested in a reply.
I realize that there are better IDE and build programs out there, I am using javac because the class I am in right now is using javac.
Now, for some extra questions to help me out:
I have the Java v6.xx updates in my add/remove programs list, would removing these in any way effect anything in a positive or negative way. And would complete removal of all JDK/JRE files and re-installation change anything?
Is there any known issues with using Java and Windows 7 that I might be running into.
Is this an issue with JRE or JDK, because it seems to be compiling without any issues but when I go to run it is when I get the errors.
Are there any other variables other than CLASS_PATH or Path that I need to set?
I appreciate any and all help that's been given so far even though I haven't found a solution yet.
Try using the absolute path to the directory where you installed java. Here is what I would type on my machine
cd "C:\Users\Sean\Documents\JAVASTUFF\Chapter 3\1"
"C:\Program Files\Java\jdk1.7.0_02\bin\javac" ch3p1.java
"C:\Program Files\Java\jdk1.7.0_02\bin\java" ch3p1
if that works, you should add the directory to the path so that this is the preffered java version. This way you won't have to use the absolute path anymore and just type "java" and "javac" as usual
set PATH="C:\Program Files\Java\jdk1.7.0_02\bin\";%PATH%
Setting the path can also be done somewhere in the Control Panel so that you don't have to type that in each time you open a command prompt
Edit: You should probably set JAVA_HOME as well as "duffymo" suggests. If it still doesnt work there may be something wrong with your java installation. Try downloading and installing a new version from Oracle
Let's start by saying you should never, ever put any of your code in the directories where you installed Java JDK. Please make it a habit to create a separate folder (e.g. "c:\work") and put your project sub-folders there.
Open a command shell and type this: java -version. That will tell you the version of Java you're compiling and running with. You care about the JDK, because you're developing code.
Is your JAVA_HOME environment variable set? Please point it to the directory where you installed Java. Then add %JAVA_HOME%\bin to your PATH. If you do all this correctly, you should be able to compile in a command shell by typing javac.
So, create a folder c:\work\learning\src and put your ch3p1.java folder in it. Create a directory c:\work\learning\classes, too. Compile your code with this command:
javac -cp .;classes -d classes src\*.java
You should see ch3p1.class in the classes folder.
Run it with this command:
java -cp .;classes ch3p1
You'll see "Hello." in the console.
EDIT: I'd recommend that you read this tutorial carefully:
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
You aren't the first person to compile and run "hello world". It seems to me that you're thrashing around too much. Simplify.
Several things here.
Java compiler can take a "target" argument to indicate for which Java version you wish to compile. By default, the target is the version of the current JVM.
So far, any class compiled with a given target is compatible with that target and any above version. So a class compiled for Java 5 is compatible with Java 5, 6 and 7.
To find out which version of Java you are running, type java -version in your terminal. It will output something like "Java 1.X..." The 'X' is the version of Java and you will often see people talking about Java 5 or Java 1.5 which is the same thing (same goes for 6 and 7).
The version that is running depends on several parameters, but usually it all comes down to the first Java bin directory in your 'PATH' environment variable.
Now, if you are considering Java IDE, there are 3 major ones:
Eclipse
NetBeans
IntelliJ
All three are fine Java IDE's and it is pretty much religious which one you will prefer. Personnally I use Eclipse.
Finally, for you problem:
Comiple using javac ch3p1.java
Run using java -cp . ch3p1
Related
I have a jar file that runs fine from the Windows 10 command line, but is not working on my desktop Ubuntu 18.04 command line. The file was exported from Eclipse as a jar, and copied to Ubuntu. The project has about 30 classes over a half dozen packages. I've done some renaming in order to simplify things for this question.
This is the command used to run the jar:
java -jar myproject.jar
On Ubuntu, I get the error "Could not find or load main class com.a.b.LaunchThis"
Following are some of the things I've tried, based on suggestions from many similar posts. Sorry if this is a duplicate, but I couldn't find a workable answer.
I verified that the current Java is 1.8 using the commands:
$ update-alternatives --config java
and
$ java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
(I also verified that the program runs using java 8 on Windows.)
I ran the following check, which displays all the files, and confirmed that com/a/b/LaunchThis.class exists and is spelled exactly the same way.
java tj myproject.jar
The manifest seems correct.
Manifest-Version: 1.0
Main-Class: com.a.b.LaunchThis
I also tried unpacking the jar and running the program directly from the folder /myproject which contains both /com and META-INF. That should work, shouldn't it?
java com.a.b.LaunchThis
java com/a/b/LaunchThis
java com.a.b/LaunchThis
And from one level outside the myproject folder:
java myproject/com/a/b/LaunchThis
java myproject.com.a.b/LaunchThis
java myproject.com.a.b.LaunchThis
java myproject/com.a.b.LaunchThis
I am not totally clear on when the syntax requires . or /. The package command in "LaunchThis" which holds the main entry point is the following:
package com.a.b;
And in each of the above iterations I also tried including "-cp myproject.jar" as an option.
Always the exact same error message.
To be careful about not having a typo in the above, I first ran the ls command to make sure the file could be found (then edited this line to create the variants).
ls myproject.com.a.b.LaunchThis.class
Any suggestions as to what else to try?
For grins, running with Java11 gets this response:
Error: Could not find or load main class com.a.b.LaunchThis
Caused by: java.lang.NoClassDefFoundError: javafx/event/EventHandler
This makes some sense, because Java11 does not include JavaFX. But Java 8 does include EventHandler. It's been a part of Java since JavaFX 2.
UPDATE: I made two "Hello world" jars, one with and one without JavaFX on an Ubuntu installation of Eclipse running OpenJDK 8. To get the version with JavaFX to run, I downloaded the Oracle JDK 1.8, and linked to /lib/etc/jfxrt.jar as an "external jar" library. Both programs run in Eclipse but only the non-FX jar works. The FX jar gives exactly the same error message.
I think this pretty much establishes my problem to be one of not having JavaFX as part of OpenJDK 8.
My plan is to now try out two solutions: (1) copying the Oracle jfxrt.jar into the OpenJDK lib, (2) backloading OpenJFX 8.
Simply installing OpenJFX from repository loads OpenJFX 11, which is a little dated but will work with the OpenJDK 11.
The issue here is that the repository OpenJDK 8 on Ubuntu 18.04 does not include JavaFX. I'm guessing that the error message results from the fact that a JavaFX main() is located on a class that extends the JavaFX class Application.
How to fix this?
There are a number of things I tested.
First, one could use Oracle's JDK 1.8 instead. I ruled this out due to licensing issues.
Some sites suggested copying the jfxrt.jar from either the Oracle JDK 1.8 or from a working Java program that uses JavaFX and uses a self-contained JRE. I found that if I linked to the Oracle jfxrt.jar (located in the /jre/lib/ext folder) as an external jar, I could run a simple, "Hello world" javafx program in Eclipse. But I could not run the program after it was exported to a jar.
One answer on a related stackoverflow thread suggested copying over several additional files along with jfxrt.jar, which I tried. This did not work, so I'm not going to pass that on.
The solution that has been most successful came from a link provided in the comment by #dave_thomson_085, How do I get Java FX running with OpenJDK 8 on Ubuntu 18.04.2 LTS?.
This answer involves purging and reinstalling openjfx with an older version, and marking it to NOT be subjected to updates.
The code provided by Wolfgang Fahl (which he credits to Druidefix) follows:
apt purge openjfx
apt install openjfx=8u161-b12-1ubuntu2 libopenjfx-jni=8u161-b12-1ubuntu2 libopenjfx-java=8u161-b12-1ubuntu2
apt-mark hold openjfx libopenjfx-jni libopenjfx-java
Now, I can run and test jars containing Java8 using JavaFX that were built on my Windows Eclipse, on my Ubuntu desktop.
I do get a warning message which I haven't dealt with yet:
Gtk-Message: 13:25:40.829: Failed to load module "canberra-gtk-module"
But this is not preventing my programs from running.
I have created an executable JAR file developed on Java version 8. The JAR file was opening on double click. But as the Oracle applications support only Java 6, I had to install JRE 6, but then after the JRE 6 installation, my executable JAR file is not opening.
I have set the JDK 8 bin path in Path environment variables. Is there a solution for this problem? Why is the JAR file not opening after two Java versions in the system?
JAR should open even if two versions 6 and 8 of Java are installed in the system.
You are facing a backward compatibility problem. Backwards compatibility means that you can run a Java 6 program on a Java 8 runtime, but not the other way around.
You can run a lower configuration on a higher configuration, not vice-versa
There are several reasons for that:
Bytecode is versioned and the JVM checks if it supports the version it finds in .class files.
Some language constructs cannot be expressed in previous versions of bytecode.
There are new classes and methods in newer JREs which won't work with older ones.
If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:
javac -source 1.8 -target 1.6 MyClass.java
You can compile your code to Java 1.6 bytecode using JDK 1.8. Just take care of the following:
-source=1.8 and -target=1.6 compiler options
If you use Maven, consider having two pom.xml files, with an optional parent file.
Source: Can program developed with Java 8 be run on Java 7?
I am not sure if this solution going to work or not.
Try to run command java -version and look if it returns java 6 or 8 path. Also try to give path of JDK 8 as JAVA_HOME variable and add that into path like this path=%JAVA_HOME%/bin and see if it works. If you get the java 6 as java version try to use above method and then install JRE 6
Hi All Thank you for your response. I kept java6 and reinstalled java8 and now forms and jar both are working!.
In the short term,
the answer is yes. Since both JDK files are downloaded as jar fils it will ok to download both jar files. The reason to not opening after two java versions is as #Elliott said: "in the system is Java 6 can't run Java 8 compiled code, you should be getting an error." That's exactly true but the problem is how to use multiple versions of JDK in a single machine.
Then we have to move on to long term,
The tricky thing is to manage these multiple JDKs and IDEs. It’s a piece of cake if I just use Eclipse for compiling my code because the IDE allows me to configure multiple versions of Java runtime. Unfortunately (or fortunately), I have to use the command line/shell to build my code. So, it is important that I have the right version of JDK present in the PATH and other related environment variables (such as JAVA_HOME).
Manually modifying the environment variables every time I want to switch between JDKs, isn’t a happy task. But, thanks to Windows Powershell, I’m able to write a script that can do the heavy lifting for me.
Basically, what you want to achieve is to set the PATH variable to add the Java bin folder and set the JAVA_HOME environment variable and then launch the correct Eclipse IDE. And, I want to do this with a single command. Let’s do it.
Open a Windows Powershell.
I prefer writing custom Windows scripts in my profile file so that it is available to run whenever I open the shell. To edit the profile, run this command: notepad.exe $profile - the $profile is a special variable that points to your profile file.
Write the below script in the profile file and save it.
function myIDE{ $env:Path = “C:vraajavajdk7bin;” $env:JAVA_HOME = “C:vraajavajdk7” C:vraaideeclipseeclipse set-location C:vraaworkspacemyproject play }
function officeIDE{
$env:Path = "C:vraajavajdk6bin;"
$env:JAVA_HOME = "C:vraajavajdk6"
C:officeeclipseeclipse
}
Close and restart the Powershell.
Now you can issue the command myIDE which will set the proper PATH and environment variables and then launch the Eclipse IDE.
As you can see, there are two functions with different configurations. Just call the function name that you want to launch from the Powershell command line (myIDE).
If any issue please put a comment below!
I have a C++ application that uses JNI to create a Java virtual machine. I have the latest version of Java installed - JDK 8-144. If I run "java -version" it comes up and tells me that Java Hotspot 8 is the JVM. I have JAVA_HOME defined in the .profile file, and it works -- JAVA_HOME points to the JDK8-144 folder.
My application is including jni.h from the latest JDK. Our code creates a JVM with the JNI_VERSION_1_8 version flag. On Windows, this compiles and works perfectly.
The problem is that when we compile this code on the Mac, and use the JVM Framework for linking, we end up with an executable that, when run, says that there's "No Java Runtime present" and a dialog comes up that says we need to install the Version 6 JVM. This happens if we run the application from either the Bash terminal or from XCode.
I've researched this and there are other people who have had this identical issue. Everyone seems to suggest that JAVA_HOME needs to be pointing to the correct place, and mine is. Again, java -version works correctly. The whole thing is quite puzzling.
Does anyone know the right way to solve this? I've tried two different Macs with identical results. They are both running the latest version of macOS Sierra.
Thanks!
Dan
I am trying to get log4j2 working in Netbeans. No matter what I do, I get the following errors
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at java.lang.ClassLoader.defineClass1(Native Method)
...
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)14
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
I am following the instructions here
Setting up log4j2
and trying to run the same demo program.
I have tried to find the solution and found this posting.
Similar Problem here
This post Class not found is an exact duplicate of my problem, but no effective answer was posted.
I have added log4j-core-2.8.2.jar and log4j-1.2-api-2.8.2.jar to the project Libraries. These two files are installed in a sub-folder of the main src folder so they are on the class path.
Netbeans is installed on a new Ubuntu build. The frustrating thing is that I was able to get this running on a Windows machine.
I tried creating a fresh new project with only this logdemo class. I added the log4j2 jar files to the Library. I also added the command -Dlog4j.configurationFile=/home/test/config/log4j2.xml
to the Run> VM Options box.
I still get exactly the same error message.
I checked my installation.
dazz#mimir:~$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
I found this post on setting Java Environment in Ubuntu. I would have thought that using apt-get would have setup the environment, but I may be wrong.
I checked /etc/environment . No mention of Java there.
No java path exported in .bash.rc either.
No java in /etc/profile.
There seems to be a number of places where the path can be set. I found this post Setting PATH variables in Ubuntu. It is modern and helpful.
I also found this post Setting Java path variables in Ubuntu
I added the following to my home profile ~/.profile
JAVA_HOME=/usr/lib/jvm/default-java
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH
Note the use of default-java. This is a symbolic link to the JDK. I hope that if/when I upgrade to the next version, apt-get will change the link.
After a reboot of Ubuntu to see if this fixed the problem, no change.
Any suggestions to find the cause of the problem would be welcome.
I installed Netbeans on my Windows 10 PC and still had the very same symptoms so I figured the problem must be with the jar files.
I found the solution. I was using log4j-core-2.8.2.jar and log4j-1.2-api-2.8.2.jar instead of log4j-api-2.3.jar and log4j-core-2.3.jar.
For whatever reason the 2.3 version works and the 2.8.2 version doesn't. When I changed the jar files the program compiled and ran under both Win 10 and Ubuntu without error.
I am very annoyed about this. I am learning Java and I like the language but I have spent far more time troubleshooting issues than writing code. The demo programm compiled OK and only threw up errors when run. The experienced programmers that make Stackoverflow such a great resource didn't spot the problem, and why should they. The cryptic error messages gave no hint to the root cause of the problem. It shouldn't be this difficult. It is very frustrating.
I compile and jar the source just fine, but when I run it, it complains:
java.lang.ClassNotFoundException: javax.swing.JPanel
I guess I have to include the Swing library when compiling, but how do I do that?
I included every rt.jar on my system:
javac -classpath /usr/lib/jvm/java-1.5.0-gcj-4.4/jre/lib/rt.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar:/home/me/equinox.jar *java
Still compiles fine, still crashes when run.
It looks like you are using GCJ. It's an old project that attempted to produce a Java implementation as pure open source.
They got about halfway, but the implementation is far from perfect. These days, it's better to avoid it entirely and instead use OpenJDK (or the Oracle/Sun JDK, if open source is not a requirement).
On Ubuntu you can use update-java-alternatives to configure your system to a different Java implementation:
sudo update-java-alternatives -s java-6-openjdk
By the way, you never need to specify rt.jar explicitly on your classpath, as it's always available automatically. Also, using multiple rt.jar from different JVMs is a recipe for disaster.
If you run the code, the compilation worked fine.
It is weird that you are missing this particular class as it is part of the standard library. In most Java distributions, this is in rt.jar
What is your current path to the JDK (in $PATH or %PATH%)? What is your whole path? What is your current CLASSPATH environment variable? Your problem is runtime environment related. You should not add rt.jar to your classpath. If you have a proper JVM install, then java doesn't need the location to rt.jar and tools.jar. I suspect that you have pathing issues to the JVM.
Also, what is the command line you are using to run the program?
You don't need to include rt.jar, it is included by the default classloader of the java vm.
You need to find out which version of java is the default one:
java --version
if you're running ubuntu, it will most probably be openjdk java, which is fine. If it's gcj, try to run it with openjdk or sun-jdk.
Also, show exactly how you run the jar.