August 19th, 2013, 04:19 PM
-
Threads and JVM error
Hi everybody,
I was programming basic threads and I haven't encountered any errors but when I try and run the program I encounter the error "A Java exception has occurred". I use two different classes one with the main method and one without to run my thread program.
I will give you the two classes Below:
Main Method:
package java.thenewboston.gamedev;
public class ThreadBasicsMain
{
public static void main(String[] args)
{
Thread t1 = new Thread(new ThreadBasics("One"));
Thread t2 = new Thread(new ThreadBasics("Two"));
Thread t3 = new Thread(new ThreadBasics("Three"));
t1.start();
t2.start();
t3.start();
}
}
All Of The Meat:
package java.thenewboston.gamedev;
import java.util.Random;
public class ThreadBasics implements Runnable
{
String name;
int time;
Random r = new Random();
public ThreadBasics(String s){
name = s;
time = r.nextInt(6666);
}
public void run()
{
try{
System.out.printf("%s is sleeping for %d\n", name, time);
Thread.sleep(time);
System.out.printf("%s is done\n", name);
}catch(Exception e){}
}
}
Thanks for any help
August 19th, 2013, 06:10 PM
-
I get no errors running your program:
Code:
scott@jalapeno:$ java ThreadBasicsMain
One is sleeping for 849
Two is sleeping for 5259
Three is sleeping for 1871
One is done
Three is done
Two is done
This was run on the command line and I removed the package statements. Can you elaborate on the error?
August 19th, 2013, 07:04 PM
-
Originally Posted by stdunbar
I get no errors running your program:
Code:
scott@jalapeno:$ java ThreadBasicsMain
One is sleeping for 849
Two is sleeping for 5259
Three is sleeping for 1871
One is done
Three is done
Two is done
This was run on the command line and I removed the package statements. Can you elaborate on the error?
This is what appears in the console error message:
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.thenewboston.gamedev
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:650)
at java.lang.ClassLoader.defineClass(ClassLoader.java:786)
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Thanks for responding
August 19th, 2013, 07:10 PM
-
Are you running this in an applet?
August 19th, 2013, 07:21 PM
-
Originally Posted by bullet
Are you running this in an applet?
I am running this through the console and it is supposed to be a simple program I don't know why it is doing this. My other projects in eclipse work fine it is just this one that is giving me troubles.
August 20th, 2013, 07:59 AM
-
I don't think the error is in your code. I believe the problem is that you are not allowed to create a class using a package that begins with the name java.
This is from the source of java.lang.ClassLoader.
Code:
if ((name != null) && name.startsWith("java.")) {
throw new SecurityException
("Prohibited package name: " +
name.substring(0, name.lastIndexOf('.')));
Comments on this post
Last edited by bullet; August 20th, 2013 at 08:19 AM.
August 20th, 2013, 03:48 PM
-
Originally Posted by bullet
I don't think the error is in your code. I believe the problem is that you are not allowed to create a class using a package that begins with the name java.
This is from the source of java.lang.ClassLoader.
Code:
if ((name != null) && name.startsWith("java.")) {
throw new SecurityException
("Prohibited package name: " +
name.substring(0, name.lastIndexOf('.')));
Alright thanks for the help. I was wondering about that myself but I didn't know that would cause that type of problem.