error executing java class file - java

I have the following file ~/workspace/A/A.java:
package A;
public class A{
public static void main(String[]args){
System.out.println("Hello world");
}
}
compiling with ecj don't give me problems,but then I get this error when tring to execute:
~/workspace/A $ java A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
~/workspace/A $ java -cp . A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I tried this on Linux Mint 17 with openjdk-7-jre version 7u55-2.4.7-1ubuntu1

As you said you have the structure /workspace/A/A.java
just open the terminal and browse to workspace directory then type the following command
For compiling
javac A/A.java
For executing
java A.A

did you try compiling your class first and then executing it
javac A.java
java A

1st you have to compile java program, using below command.
javac A.java
Then, run java A

First you need to compile using javac A.java
then you also need to specify classpath while running like
java -classpath . A

Don't know if ecj differs from javac, so make sure the A.class file has ended up in same dir as your java file (~/workspace/A/) after compiling and try this:
cd ~/workspace
java -cp . A.A

I recommend using a JAVA package name in lowercase especially when your classname is same as the package name. You might want to read this:
http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

Related

How to fix Unsupported major.minor version 52.0 Exception?

I have a spring boot REST API jar file which works perfectly fine when I run it in my local machine using eclipse IDE, however when I upload the jar file to my VPS server I get the following error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/JarLauncher : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I changed the java version in my POM file from version 11 to java 8:
<properties>
<java.version>8</java.version>
</properties>
In the VPS server I could see that the available java versions are: java 7 (Version : 7u80) and java 8 (Version : 8u92 - 1.8.0_92-b14), however my JRE version is jre 1.8.0_201.
If I keep java 7 in the VPS server I get the exception mentioned above and if I remove java 7 from the VPS server, then I get the following error when trying to run the jar file from ssh using Putty:
[root#vps public_html]# java -jar SpringBootProject-0.0.1-SNAPSHOT.jar
-bash: java: command not found
[root#vps public_html]#
How do I fix this?

Java Exceptions error when trying to execute .jar on Debian Jessie

A teacher of mine created a program on java around 2005, he has always used MacOS. He sent the .jar to me and a classmate, both linux (me Debian 8 and him Ubuntu) and we can't execute the program because it returns a lot of exceptions.
At first I thought it could have been because the program was too old but my teacher can run it in his 1 year old updated mac perfectly fine.
Checked and Debian comes with OpenJDK as default. Havent installed anything, is there anything I should be installing for it to run?
PS: if I double click the .jar I can see the folders.
The exceptions are:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/chart/ChartPanel
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.jfree.chart.ChartPanel
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 18 more
I don't know anything about java so please if you can try to be as basic as you can. I am also new to Debian, so I really don't know if there's a package missing from the default installation.
Edit:
The MANIFEST.MF file is
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_24-b07-334-10M3326 (Apple Inc.)
Class-Path: lib/Jama-1.0.2-ed.jar lib/jfreechart-1.0.6.jar lib/ant.jar
lib/jcommon-1.0.10.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.cmcweb.electronicstructure.CompleteDOSPlotter
You have to download JFreeChart.jar libary (http://www.java2s.com/Code/Jar/j/Downloadjfreechartjar.htm) because your teacher didn't include external classes in his jar file.
Put JFreeChart.jar into /your_linux_path/jre/lib/ext folder and after that it should works.
P.S. The same you have to do with Jama.jar and jcommon.jar
Make sure you have all jars with the version from the manifest file downloaded in a lib directory. Lib directory should be placed in the same directory as your jar, the names of dependency jars should be exactly the same as these from classpath in manifest.
So in your case, it may not work with the newest JFreeChart version. You need to find and download version 1.0.6

ClassNotFoundException Error any ideas?

I am new to Java and I get this error for my code:
C:\Users\Unknown\Desktop\AIB>java GreenhouseControls -f examples1.txt
Restarting system
java.lang.ClassNotFoundException: ThermostatNight
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at GreenhouseControls$Restart.action(GreenhouseControls.java:139)
at tme3.Controller.run(Controller.java:32)
at GreenhouseControls.main(GreenhouseControls.java:197)
I believe I have a classpath errors.
Did you try to add your compiled java files (i.e., Event.class, ThermostatNight.class, ...) of Event.java, ThermostatNight.java to the classpath:
java -cp "target/*" GreenhouseControls -f examples1.txt
I assume that your compiled class files are in the target directory.
I solved in this way.
java -classpath YourProjectWar.jar com.yourpackage.ClassWithMain -f examples1.txt

Apache Ant: NoClassDefFoundError when using Junit

I just can't seem to get my junit tests run by Ant. Here's my setup:
build.xml
http://pastebin.com/c7n4cdJ6
What am I trying to do? I run ant main or ant package respectively. The compile target compiles my code. First the classes of my webapp and afterwards my test classes:
javac: source: webapp java files, destination: ${web.classes.dir}
javac: source: test java files, destination: ${web.testclasses.dir}
For dependency management I use Apache Ivy, which is working fine.
So let's take a look at my junit target. You can see that the classpath is defined in junit.classpath. It contains:
My Ivy dependencies with the test scope: <path refid="test.path"/>
The compiled class files of my tests: <pathelement location="${web.testclasses.dir}"/>
The compiled class files of my webapp: <pathelement location="${web.classes.dir}"/>
Also it contains a dirset of my compiled test classes (you see I was desperate).
So when I run the ant script it compiles well, the class files are all there. But when it comes to junit target and the first test is being executed, I get this error in the console:
[junit] Running CreateStopwordSqliteDBTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
BUILD FAILED
In the result xml file I get further information:
<error message="CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)" type="java.lang.NoClassDefFoundError">
java.lang.NoClassDefFoundError: CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
</error>
NoClassDefFoundError tells me that the class could be found at compile time but not on runtime. Please note that the affected class is the actual JUnit test. Why can't it be found even though I made sure it is part of my junit classpath?
Fyi: When I remove the lines 131 - 133 from my build.xml (the dirset in my junit classpath), I would get a ClassNotFoundException:
<error message="CreateStopwordSqliteDBTest" type="java.lang.ClassNotFoundException">
java.lang.ClassNotFoundException: CreateStopwordSqliteDBTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
</error>
Could you help me out here?
The source directories are wrong. Here are the correct properties:
<property name="src.dir" location="${basedir}/src" />
<property name="testsrc.dir" location="${basedir}/testsrc" />

Jruby error (wrong name: ruby/jit/FILE_D40BB14F6D953C9F0017A388A07364EC0E5B3F6E)

I am trying to compile a file using jruby with the following commands but I have not had any luck.
jruby -S jrubyc test5.rb
java -cp .:/Users/Ted/.rvm/rubies/jruby-1.6.2/lib test5
I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: test5 (wrong name: ruby/jit/FILE_D40BB14F6D953C9F0017A388A07364EC0E5B3F6E)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:698)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Ted
You need to package your class in a jar file.
Also, your -cp argument needs to specify an actual jar file (or zip) - it cannot refer to a directory as in your example.

Categories

Resources