Why can't I load this class file in Rhino console? - java

I'm prototyping a web page scraper using Rhino and Env-js. Nevermind that the documentation for both projects is atrocious... I'm trying to load up the File.java example class that is supplied with Rhino. For simplicity sake, I've got File.java, js.jar, jline.jar and env.rhino.1.2.js all in one directory. I've tried specifying the current directory using the classpath command line option, but still whenever I call defineClass("File") I get an error saying the class file isn't found. What am I doing wrong here??
$ ls -1
File.java
env.rhino.1.2.js
jline.jar
js.jar
$ java -cp .:js.jar:jline.jar jline.ConsoleRunner org.mozilla.javascript.tools.shell.Main -opt -1
Rhino 1.7 release 2 2009 03 22
js> defineClass("File")
js: "<stdin>", line 2: Class "File" not found.
at <stdin>:2

Don't you need to compile File.java before using it, as the classpath "." only makes sense if it contains some compiled class in it?

Related

Java packages: no class def found

so I've tried many of the solutions that are present in this website but none could help me.
The problem:
I have my project files structured like this:
cnv/webserver/aplication/insProj.java
cnv/webserver/aplication/IntFactorization.java
and both are in the package webserver.aplication.
to compile and run i do this:
starting at the folder cnv and with the $CLASSPATH=/home/ll/Documents/cnv
cd webserver/aplication
javac -source 1.4 insProj.java
javac IntFactorization.java
cd ..
cd ..
java -XX:-UseSplitVerifier webserver.aplication.insProj ./webserver/aplication/IntFactorization.class
java -XX:-UseSplitVerifier webserver.aplication.IntFactorization 5
When executing this last instruction, it returns an exception:
Factoring 5...
Exception in thread "main" java.lang.NoClassDefFoundError: webserver.aplication.insProj
at webserver.aplication.IntFactorization.calcPrimeFactors(IntFactorization.java:22)
at webserver.aplication.IntFactorization.main(IntFactorization.java:59)
The instrumentations i'm making is incrementing a counter when a given method is executed and creating a file when the class ends execution. I know this works because i've tested outside these packages and it works fine.
It seems BIT doesn't work well with packages. In the end, I removed insProj from any package and inserted its folder in the classpath. Then it began to work.

"Error: Could not find or load main class” in jenkins

i am using jenkins in ubuntu and i need to call a java class from python script. The code:
import os
import shutil
import sys
from subprocess import call, STDOUT
param1=os.getenv(‘PARAM1’)
param2=os.getenv(‘PARAM2’)
param3=os.getenv(‘PARAM3’)
cmd1 =”cp /…/Class.class $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/Class.class ”
cmd2=”java $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/Class ” +””+param1+””+param2””+param3
print>>> sys.stder, “Launching command: “ + cmd2
call(cmd1,shell=True)
call(cmd2,shell=True)
But the console output shows “Error: Could not find or load main class”
I have checked an the file was copied, and Jenkis have installed the Java SE Development Kit 8u31 version.
I have try build the process in two step, first copy the java file and later set up the variables and do the second call but appears the same error.
Thanks,
i have changed the code to:
classpath=os.path.join(os.getenv('JENKINS_HOME'),"jobs",os.getenv(JOB_NAME'),"builds",os.getenv('BUILD_NUMBER'))
cmd2=[“java”,”-classpath”,classpath,”Class”,param1,param2,param3]
call(cmd2)
The code Works!!!
When i build with parameters the console output shows "Usage_ java [- options] class [args...]..."
Java doesn't support "run this file as a class" directly. Instead, you need to add the class to the classpath and then call it using the Java Fully Qualified name:
java -classpath $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER com.foo.Class ...
would run the Java code in .../builds/$BUILD_NUMBER/com/foo/Class.class
Note: Avoid call() with a string. Instead build a list of command plus arguments. That way, you can replace variables correctly and spaces in file names won't cause unexpected/hard to find problems:
classpath = os.path.join(os.genenv("JENKINS_HOME), "jobs", ...)
cmd = [
"java",
"-classpath",
classpath,
"Class",
...
]
call(cmd)

Java class will not run (command prompt) [duplicate]

This question already has answers here:
Exception in thread "main" java.lang.NoClassDefFoundError: wrong name
(3 answers)
Closed 9 years ago.
I have been fighting with this program for a little while now and cannot figure out what is wrong. Any help would be greatly appreciated.
So here's the issue. I have three classes one is for logging onto a mysql database, the other is to output data from the database, and the last one holds method main. I was having a huge issue with getting them to compile getting errors about not finding a symbol for a method in a different class. I finally got them to all compile by using command "javac -d bin/cdtPack src/CDT.java src/login.java src/ClientBase.java"
But, now when I try to run the class with method main I get error:
Exception in thread "main" java.lang.noClassDefFoundError: CDT (wrong
name: cdtPack/CDT)
then a list of at java....
Anyone have an idea what could be going wrong?
As the linked Q&A explains this happens when you try to run a Java application using the wrong class name.
Your class looks something like this:
package cdtPack;
public class CDT {
....
}
That means that its class name is "cdtpack.CDT".
But you are running it like this:
$ java CDT
The JVM is telling you this:
"You told me to run CDT, but when I looked at it, the class said its name is "cdtpack.CDT"!!"
You have to get your head around the way that the Java classpath works, and the way that javac and java and all of the other Java tools find classes.
Your "CDT.class" file should be in a directory called "cdtpack", and then "cdtpack"'s parent directory should be on the classpath; i.e.
Compile like this:
$ javac -d bin -classpath bin src/cdtpack/CDT.java
which should create "bin/cdtpack/CDT.class". Then run like this:
$ java -classpath bin cdtpack.CDT
It looks like CDT should either belong in the package cdtPack or you are running it from the wrong directory...
Try changing into the bin directory and run the class file again. Don't forget to include the package name before the class name. For example:
...\bin> java cdtPack.CDT
try this
you goto src directory and if class file available there, then type
java CDT
if the class is present in some other directory then type
java a/b/c/JavaClassName
if you want to add some runtime jar while running then
java -cp classpath=%classpath%;jarfilename.jar; a/b/c/JavaClassName

javac error: Class names are only accepted if annotation processing is explicitly requested

I get this error when I compile my java program:
error: Class names, 'EnumDevices', are only accepted if annotation
processing is explicitly requested
1 error
Here is the java code (I'm running this on Ubuntu).
import jcuda.CUDA;
import jcuda.driver.CUdevprop;
import jcuda.driver.types.CUdevice;
public class EnumDevices {
public static void main(String args[]) {
CUDA cuda = new CUDA(true);
int count = cuda.getDeviceCount();
System.out.println("Total number of devices: " + count);
for (int i = 0; i < count; i++) {
CUdevice dev = cuda.getDevice(i);
String name = cuda.getDeviceName(dev);
System.out.println("Name: " + name);
int version[] = cuda.getDeviceComputeCapability(dev);
System.out.println("Version: " +
String.format("%d.%d", version[0], version[1]));
CUdevprop prop = cuda.getDeviceProperties(dev);
System.out.println("Clock rate: " + prop.clockRate + " MHz");
System.out.println("Threads per block: " + prop.maxThreadsPerBlock);
}
}
}
Here is the javac command:
javac -cp /home/manish.yadav/Desktop/JCuda-All-0.3.2-bin-linux-x86_64 EnumDevices
How do I compile this program?
You at least need to add the .java extension to the file name in this line:
javac -cp /home/manish.yadav/Desktop/JCuda-All-0.3.2-bin-linux-x86_64 EnumDevices
From the official faq:
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.
Also, in your second javac-example, (in which you actually included .java) you need to include the all required .jar-files needed for compilation.
I was stumped by this too because I was including the .Java extension ... then I noticed the capital J.
This will also cause the "annotation processing" error:
javac myclass.Java
Instead, it should be:
javac myclass.java
Using javac ClassName.java to compile the program,
then use java ClassName to execute the compiled code. You can't mix javac with the ClassName only (without the java extension).
The error "Class names are only accepted if annotation processing is explicitly requested" can be caused by one or more of the following:
Not using the .java extension for your java file when compiling.
Improper capitalization of the .java extension (i.e. .Java) when compiling.
Any other typo in the .java extension when compiling.
When compiling and running at the same time, forgetting to use '&&' to concatenate the two commands (i.e. javac Hangman.java java Hangman). It took me like 30 minutes to figure this out, which I noticed by running the compilation and the running the program separately, which of course worked perfectly fine.
This may not be the complete list of causes to this error, but these are the causes that I am aware of so far.
I learned that you also can get this error by storing the source file in a folder named Java
chandan#cmaster:~/More$ javac New.java
chandan#cmaster:~/More$ javac New
error: Class names, 'New', are only accepted if annotation processing is explicitly requested
1 error
So if you by mistake after compiling again use javac for running a program.
How you can reproduce this cryptic error on the Ubuntu terminal:
Put this in a file called Main.java:
public Main{
public static void main(String[] args){
System.out.println("ok");
}
}
Then compile it like this:
user#defiant /home/user $ javac Main
error: Class names, 'Main', are only accepted if
annotation processing is explicitly requested
1 error
It's because you didn't specify .java at the end of Main.
Do it like this, and it works:
user#defiant /home/user $ javac Main.java
user#defiant /home/user $
Slap your forehead now and grumble that the error message is so cryptic.
Perhaps you may be compiling with file name instead of method name....Check once I too made the same mistake but I corrected it quickly .....#happy Coding
first download jdk from https://www.oracle.com/technetwork/java/javase/downloads/index.html.
Then in search write Edit the System environment variables
In open window i push bottom called Environment Variables
Then in System variables enter image description here
Push bottom new
In field new variables write "Path"
In field new value Write directory in folder bin in jdk like
"C:\Program Files\Java\jdk1.8.0_191\bin"
but in my OS work only this "C:\Program Files\Java\jdk1.8.0_191\bin\javac.exe"
enter image description here
press ok 3 times
Start Cmd.
I push bottom windows + R.
Then write cmd.
In cmd write "cd (your directory with code )" looks like C:\Users\user\IdeaProjects\app\src.
Then write "javac (name of your main class for your program).java" looks like blabla.java
and javac create byte code like (name of your main class).class in your directory.
last write in cmd "java (name of your main class)" and my program start work
To avoid this error, you should use javac command with .java extension.
Javac DescendingOrder.java <- this work perfectly.
I created a jar file from a Maven project
(by write mvn package or mvn install )
after that i open the cmd , move to the jar direction and then
to run this code the
java -cp FILENAME.jar package.Java-Main-File-Name-Class
Edited : after puting in Pom file declar the main to run the code :
java -jar FILENAME.JAR
If you compile multiple files in the same line, ensure that you use javac only once and not for every class file.
Incorrect:
Correct:

Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line

I have a problem while trying executing my java application.
Whenever I try to execute the program through the command
java ProgAudioJ
I get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
at java.lang.ClassLoader.defineClass1(NativeMethod)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
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(NativeMethod)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ProgAudioJ. Program will exit.
If I remove from my code:
package es_2011;
Everything works perfectly. How do I solve the problem?
Because I found these answers unclear, here is what you need to do.
First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):
cd /myFolder/myPackage
javac MyClass.java
cd ..
java myPackage.MyClass
Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like
package com.somedomain.someproject;
you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):
cd /myCode
java com.somedomain.someproject.MyMainClass
Try using:
java es_2011.ProgAudioJ
(instead of java ProgAudioJ).
I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.
Try this (compile and run):
dir
2011-02-10 00:30 <DIR> .
2011-02-10 00:30 <DIR> ..
2011-02-10 00:27 58 es_2011
javac es_2011/ProgAudioJ
java es_2011.ProgAudioJ
It's quite clearly stated there:
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,
src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;
(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.
Try this,
Compile your class using below command
$ javac ProgAudioJ.java -d .
Run your application by command
$ java es_2011.ProgAudioJ
The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.
I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file.
It has been packaged up as
es_2011/ProgAudioJ
In order to run it - you have to either move up a directory:
If you are here: (Windows)
src\es_2011\
move to
src\
Then run the line:
java es_2011.ProgAudioJ
This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).

Categories

Resources