Suppose I have the following interface deceleration :
public interface classListener<classEvent> {
void classMethod1(classEvent x);
void classMethod2(classEvent y);
void classMethod3(classEvent z);
}
and suppose I want to add this interface to whatever.something package.
What should i do ?
Put a package statement at the top of the file:
package your.package.name;
public interface classListener<classEvent> {
void classMethod1(classEvent x);
void classMethod2(classEvent y);
void classMethod3(classEvent z);
}
suppose I want to add this interface to java.something package
You cannot add to the java. packages. That's just for the JDK (currently controlled by Oracle since they acquired Sun). It'll compile, but when you try to use it you'll get a SecurityException:
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:479)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:625)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: UsePackage. Program will exit.
My code that gave me that error:
In ~/temp/java/util/AddPackage.java:
package java.util;
public interface AddPackage {
void doSomething();
}
In ~/temp/UsePackage.java:
import java.util.AddPackage;
class UsePackage implements AddPackage {
public static final void main(String[] args) {
new UsePackage().doSomething();
}
public void doSomething() {
System.out.println("Hi");
}
}
Commands:
~/temp$ javac java/util/AddPackage.java
~/temp$ javac UsePackage.java
~/temp$ java UsePackage
...whereupon the error occured. Using Oracle's (Sun's) JVM.
The package decl creates a namespace for the content below. Just add a package decl as first line in your interface file.
package org.whatever.foo
public interface ....
Related
The compiler for a JVM-based programming language currently that I am working on uses this code to run a specified main method after compilation:
URL url = DyvilCompiler.config.outputDir.toURI().toURL();
Class c = Class.forName(mainType, false, new URLClassLoader(new URL[] { url }, ClassLoader.getSystemClassLoader()));
Method m = c.getMethod("main", String[].class);
m.invoke(null, new Object[] { args });
However, when compiling this code:
package dyvil.test
// some uninteresting import stuff
public class Main
{
#ArrayConvertible
case class IntVector([int] ints = [ 1, 2 ])
public static void main([String] args)
{
println(IntVector())
println([ 1, 2, 3 ] as IntVector)
}
}
The ClassLoader fails with the inner class (that is actually a nested static class):
java.lang.ClassFormatError: Illegal class name "Ldyvil/test/Main$IntVector;" in class file dyvil/test/Main$IntVector
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at dyvil.test.Main.main(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at dyvil.tools.compiler.util.TestThread.run(TestThread.java:32)
Note that the class files generated for Main dyvil/test/Main and IntVector dyvil/test/Main$IntVector do define the inner class relation:
Inner classes:
[inner class info: #2 dyvil/test/Main$IntVector, outer class info: #7 dyvil/test/Main
inner name: #9 IntVector, accessflags: 8 static]
Enclosing Method: #7 #0 dyvil/test/Main
What is the problem with the Ldyvil/test/Main$IntVector; signature?
Also, when adding the output directory to the classpath and changing the code to
Class c = Class.forName(mainType)
everything works well without any errors.
EDIT: Not only does this cause problems with the JVM, but when using a class like this in Eclipse by importing it, Code Completions stop working completely.
I have a maven project in the following structure.
TestRun
|
|--src/main/java/com/main/CallAddNumbers.java (Package - Com.main)
|
|--src/test/java/com/test/RunTest.java (package com.test)
and here is the sample program
package com.main;
public class CallAddNumbers {
public static void main(String[] args) {
com.test.AddNumbers.addNumbers(5, 4);
}
}
package com.test;
public class AddNumbers {
public static void addNumbers (int a, int b){
System.out.println(a+b);
}
}
When I am calling addNumbers method from main, I am ending up with the following error. May be something simple, but can't figure it out.
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/RunTest
at com.main.CallAddNumbers.main(CallAddNumbers.java:6)
Caused by: java.lang.ClassNotFoundException: com.test.RunTest
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)
... 1 more
you said it might be something simple, just wanted to check that this is your actual file structure before looking any further!
|
|--src/main/java/com/main/CallAddNumbers.java (Package - com.main)
|
|--src/test/java/com/test/addNumbers.java (package com.test)
Add this import statement to the CallAddNumbers class
import test.java.com.test.AddNumbers;
It will work fine. Btw better dont write production codes in test package
I would like to call the following Scala code in Java:
Scala code
package calculate
import java.io._
class CalculationScala
object CalculationScala {
def main(args: Array[String]) {
def operate(a: Double, b: Double, op: (Double, Double) => Double): Double = (op(a, b))
println(operate(5, 15, _-_))
}
}
Java code
package calculate;
public class Calculation {
public static void main(String[] args) {
CalculationScala calculationScala = new CalculationScala();
calculationScala.main(args);
}
}
but the following error occurs.
Error
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function2
at calculate.CalculationScala.main(CalculationScala.scala)
at calculate.Calculation.main(Calculation.java:79)
Caused by: java.lang.ClassNotFoundException: scala.Function2
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)
... 2 more
How to solve this issue?
It looks like you are missing scala-library.jar on your runtime classpath.
I am having trouble running a java program that works fine in the IntelliJ IDEA ide. The error I get when I run the same code (after removing the package ..) as follows
Exception in thread "main" java.lang.NoClassDefFoundError: fcrypt
Caused by: java.lang.ClassNotFoundException: fcrypt
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
All I'm doing in the main method is creating an instance of the main class and calling the several methods. The code with just the headers and the main method as below
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
/**
* Created by Aditya Rao on 05/02/14.
*/
public class fcrypt {
private static final String RSA_NONE_PKCS1PADDING = "RSA/None/PKCS1Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
....
public static void main (String[] args) throws Exception {
if (args.length != 5) {
System.out.print("Invalid parameters. ");
printUsage();
System.exit(1);
}
if (!(args[0].equals("-e") | args[0].equals("-d"))) {
System.out.print("Please specify usage. ");
printUsage();
System.exit(1);
}
fcrypt f = new fcrypt();
String[] inputs = Arrays.copyOfRange(args, 1, args.length);
if (args[0].equals("-e"))
f.encryptAndSign(inputs);
else
f.verifyAndDecrypt(inputs);
}
}
Am I missing something here?
EDIT I compile and run this program with the following commands
javac -cp libs/bcprov-jdk15on-150.jar fcrypt.java
java -cp libs/bcprov-jdk15on-150.jar fcrypt <args>
You have to add working directory denoted as . to the class path as fcrypt.class is located there.
Syntax for Unix:
java -cp ".:libs/bcprov-jdk15on-150.jar" fcrypt
note elements are separated with :.
Syntax for Windows:
java -cp ".;libs/bcprov-jdk15on-150.jar" fcrypt
note elements are separated with ;.
Java code style suggests class names to start with a capital letter. So it should be class FCrypt defined in FCrypt.java.
Why does the following code cause ClassNotFoundException?
public class App02 {
public static class A {
}
public static void main(String[] args) throws ClassNotFoundException {
try {
System.out.println("A.class.getCanonicalName() = " + A.class.getCanonicalName());
Class c = Class.forName("tests.App02.A"); //error on this line
System.out.println(c.getName());
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Output:
A.class.getCanonicalName() = tests.App02.A
java.lang.ClassNotFoundException: tests.App02.A
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at tests.App02.main(App02.java:15)
Try Class c = Class.forName("tests.App02$A"). It's not a top-level class, so use $ to locate it.
You need to use $ to access the nested class:
Class c = Class.forName("tests.App02$A");
When you compile your class, you will notice that the nested class is named as: App02$A.class, under package tests. It would make more sense then.
Because you are using a canonical name, but you should use name (A.class.getName()).
In your case you should use Class c = Class.forName("tests.App02$A");
There is a helpful util in commons-lang which support these classes:
org.apache.commons.lang3.ClassUtils.get("tests.App02.A")