I have 2 java program located seperately
One in c:\test and the other in c:\test\new
I can compile both of it without any error \javac
But when i try to execute the file \java
it shows the error like this
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ButtonFrame.makeButton(ButtonTest3.java:42)
at ButtonFrame.<init>(ButtonTest3.java:29)
at ButtonTest$1.run(ButtonTest.java:17)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
i put this in my classpath
CLASSPATH value- C:\test;C:\test\new
but if i change the order of the value in CLASSPATH to this
CLASSPATH value- C:\test\new;C:\test
the error is simply gone
Why?? this could happening
Only the order matters?
You've a class with the same name in the both folders. In C:\test there's a version of the ButtonTest3 class which contains a programming bug causing this NullPointerException. In C:\test\new there's a different version of the ButtonTest3 class which doesn't contain this bug, or probably there's a ButtonTest class which does entirely different things than the one in C:\test.
Cleanup your classpath. It's not good to have duplicate different versioned classes with the same signature in the classpath. If your intent is that new is supposed to be a package identifier, then you need to leave it away from the classpath. However, such a package name would have resulted in a compilation error, so that can't be it.
As to the bug, a NullPointerException is relatively trivial to naildown and fix. First look at the first line of the stacktrace:
at ButtonFrame.makeButton(ButtonTest3.java:42)
It's telling that it has occurred in line 42 of ButtonTest3 class, inside the makeButton() method. Now go to line 42 of ButtonTest3.java, it'll look something like:
someObject.doSomething();
Look there where a dot operator . is been used to invoke a method or access a field of some object. The NullPointerException means that someObject is null at the particular moment. There is no instance!
It's an easy fix: just ensure that it is not null at the moment you're invoking/accessing it:
someObject = new SomeObject();
// ...
someObject.doSomething();
Well, I don't believe you can have two classes defined in a single source file. You can have them defined as a subclass.
According to the Java spec:
Each class file contains the
definition of a single class or
interface. Although a class or
interface need not have an external
representation literally contained in
a file (for instance, because the
class is generated by a class loader),
we will colloquially refer to any
valid representation of a class or
interface as being in the class file
format.format.
You could place ButtonFrame inside ButtonTest2.
public class ButtonTest2
{
public static void main(String[] args)
{
...
ButtonFrame frame = new ButtonFrame();
}
class ButtonFrame extends JFrame {
....
}
}
Or, put them in different java files.
You have two classes at top level in program, thats wrong. But keeping that aside, your program is not getting compiled at first place.
To successfully compile the program use the following NppExec script:
cmd /c cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\javac" "$(FULL_CURRENT_PATH)"
cmd /k cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\java" "$(NAME_PART)" && exit
Make sure that you have your JDK folder set to JAVA_HOME environment variable.
and try again.
Related
Can any body suggest me which jar i shld use to resolve the below exception,
Exception in thread "main" java.lang.NoSuchMethodError: org.neo4j.graphdb.factory.GraphDatabaseSetting$BooleanSetting.<init>(Ljava/lang/String;)V
at org.neo4j.shell.ShellSettings.<clinit>(ShellSettings.java:37)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.neo4j.kernel.configuration.AnnotatedFieldHarvester.findStatic(AnnotatedFieldHarvester.java:47)
at org.neo4j.kernel.configuration.AnnotationBasedConfigurationMigrator.<init>(AnnotationBasedConfigurationMigrator.java:40)
at org.neo4j.kernel.configuration.Config.<init>(Config.java:89)
at org.neo4j.kernel.InternalAbstractGraphDatabase.<init>(InternalAbstractGraphDatabase.java:218)
at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:103)
at org.neo4j.graphdb.factory.GraphDatabaseFactory$1.newDatabase(GraphDatabaseFactory.java:88)
at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:207)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.newEmbeddedDatabase(GraphDatabaseFactory.java:69)
at com.Neo4J.src.EmbeddedNeo4j.createDb(EmbeddedNeo4j.java:48)
at com.Neo4J.src.EmbeddedNeo4j.main(EmbeddedNeo4j.java:38).
I have already included the below jar,
neo4j-kernel-1.9.3.jar.
Thanks.
Make sure you have the matching neo4j-shell jar file.
The command javap -v -classpath neo4j-shell-1.9.3.jar org.neo4j.shell.ShellSettings | grep invoke indicates to me that there are only five methods called by ShellSettings. One is the Object default constructor, but that's during the constructor for ShellSettings, not the static class initialization <clinit>. The others are all from class org.neo4j.helpers.Settings. setting is called four times, in two different overloaded versions, matches and illegalValueMessage each once.
So in the 1.9.3 version of neo4j, the call to the constructor of GraphDatabaseSetting$BooleanSetting which is mentioned in your stack trace does not occur. therefore your ShellSettings class must come from some other version which is not binary compatible to 1.9.3. Use compatible versions, and you should be fine.
I wanted to instrument some methods/classes in Java. For that purpose I wrote an Java agent that instruments only a few classes that I choose.
I use the following code to get the loaded classes and filter some of them:
instrumentation.addTransformer(myTransformer,true);
Class[] loadedClasses=instrumentation.getAllLoadedClasses();
Class[] modifiableClasses=ModifiableClasses(loadedClasses,instrumentation);
Class[] filteredClasses=filterClasses(modifiableClasses);
if(instrumentation.isRetransformClassesSupported()){
System.out.println("retransformation is Supported");
instrumentation.retransformClasses(filteredClasses);
}
this should not create any problem filterClasses returns some classes that I want to be instrumented normally I want to instrument these Classes:
Ljava/nio/Buffer;
Ljava/util/HashMap$Entry;
Ljava/nio/HeapCharBuffer;
Ljava/nio/CharBuffer;
Ljava/lang/ClassLoader;
Ljava/lang/Class;
Ljava/util/HashMap;
Ljava/nio/ByteBuffer;
Ljava/lang/System;
Ljava/io/BufferedWriter;
Ljava/lang/String;
Ljava/io/OutputStreamWriter;
Ljava/io/BufferedOutputStream;
Ljava/nio/charset/CoderResult;
Ljava/io/Writer;
Ljava/util/HashSet;
Ljava/nio/charset/CharsetEncoder;
Ljava/io/FileOutputStream;
Ljava/io/PrintStream;
everything works fine I print the instrumented and unistrumented classes in specific folders for debug. I use the asm CoreAPI to add some instructions at the beginning and the end of methods I dont instrument Constructors,native methods, abstract methods and static variables. When I just instrument the new classes that are being loaded everything works fine. I guess I dont understand something with the ClassLoader or something specific here is what I get on the Console:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
Caused by: java.lang.InternalError
at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at sun.instrument.InstrumentationImpl.retransformClasses(Unknown Source)
at my.Agent.Watcher.premain(Watcher.java:88)
... 6 more
FATAL ERROR in native method: processing of -javaagent failed
I am thankful for any help I can get even a link or a hint in the right direction.
Sorry guys my mistake after looking more precisely in the instrumented classes I found Errors which I will have to solve, if I want this to work. An admin/mod can close this thread .
I created a new maven project in Eclipse and on runtime I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: =. Program will exit.
In other threads the class is mentioned where the problem occurs but here it simply says nothing.
The code is also used in a different project (with slight tweaks in terms of calling a method) but the rest of it is same.
If anyone can help me resolve this issue..it will be highly appreciated.
It looks like something is passing in = as the class name. It doesn't say nothing - it says =.
For example, when I run:
java =
I get:
Error: Could not find or load main class =
There's no colon, but it's otherwise the same.
Look at where you're trying to specify the class name, and see whether there's a stray = around. For example, suppose you had:
java -Dfoo = bar ClassName
instead of
java -Dfoo=bar ClassName
You'd see the same thing. I'm not familiar with Maven, but if you ever specify a set of arguments in it, I'd look at that part of the configuration file.
Deleting the workspace worked for me.
I have the zend javabridge working. Now I want to call a .jar file myWebTest.jar with an class of the same name. Then i want to call a function testWeb which returns a hello world string.
This in on 32bit Win 7 professional system
I added myWebTest.jar to the classpath located in /zend/zendserver/etc/java_bridge_server.ini:
[JAVA_BRIDGE_SERVER]
CLASSPATH="C:\Program Files\Zend\ZendServer\bin\javamw.jar;C:\Program Files\Zend\ZendServer\bin\myWebTest.jar;."
This path is correct.
I have restarted zend server and the code I am trying is:
$jObj = new Java("myWebTest");
// Print date through the object
print $jObj->testWeb("jim");
The log:
1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
[08-Dec-2011 11:44:48] PHP Fatal error: Call to a member function testWeb() on a non-object in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 6
[08-Dec-2011 11:45:51] PHP Fatal error: Uncaught exception 'JavaException' with message 'Java Exception java.lang.ClassNotFoundException: myWebTest
java.lang.ClassNotFoundException: myWebTest
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
in C:\Program Files\Zend\Apache2\htdocs\javaObject.php:4
Stack trace:
#0 C:\Program Files\Zend\Apache2\htdocs\javaObject.php(4): *No Class!*->jbridge('myWebTest')
#1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
I can't find a how to on this anywhere. TIA Jim
From the documentation of Java
object java(string $class_name [ , ... ])
Parameters
class_name: Class name to create
...: Additional arguments are treated as constructor parameters
Return Value: The Java object that was created, NULL otherwise
Not to be confused, actually class name needs to be the full qualified Name of the class. As an example, if you have a class as below:
package experiment;
public class Test {
....
}
Here class name is: Test
Fully qualified name: experiment.Test
So to instantiate this class in PHP, you need to write:
$test = new Java("experiment.Test");
Additionally according to your exception log
Java Exception java.lang.ClassNotFoundException: myWebTest
It shows that, it can not find any class named as myWebTest in your class path. Which means either there is no class named myWebTest in the jar file, or jar is not properly loaded.
The argument to Java() is the fully qualified class name of the class you want to access, not the name of the jar file. For example the php java bridge docs say,
java("java.lang.Long")->MAX_VALUE
This access the class Long, in the package java.lang
There is some info on java packages and class names here.
I want to write a classLoader that can help me implement customized classes and ultimately a whole component at run time. Right now I'm in process of loading the class.
I'm trying to load this role.java file. However when I get to this part of the code:
myClass = super.defineClass(className, classData, 0, classData.length);
I get this exception:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1885430635 in class file C:\Users\ARIFAH\Downloads\Compressed\eUML2 free version\with classLoader code\2\archetypedComponentWithNull\src\aC\Role/java
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at customCL.customClassLoader.loadClass(customClassLoader.java:116)
at java.lang.ClassLoader.loadClass(Unknown Source)
at customCL.customClassLoader.main(customClassLoader.java:145)
I've read posts saying "you need something like OSGi". That would be similar to working on something new from scratch, which I'd like to avoid.
Why am I getting this error?
You aren't actually loading a real class file. The magic value of any valid class file is 0xCAFEBABE, and this magic in hex is 0x7061636B.
Notice that if we convert 0x7061636B to ASCII byte by byte, it turns out to be the string "pack". This means that the file you think is a class file actually starts with the string "pack".