Error when generating JNI header file - java

I'm trying to generate the JNI header file through command line. when I
typed javah myclass.class, the errors was
Exception in thread main java.IllegalArgumentException: not a valid class name
at com.sun.tools.javac.api.JavacTool.getTask<JavacTool.java:177>
at com.sun.tools.javac.api.JavacTool.getTask<JavacTool.java:68>
at com.sun.tools.javah.api.JavachTask.run<JavahTask.java:509>
at com.sun.tools.javah.api.JavachTask.run<JavahTask.java:335>
at com.sun.tools.javah.Main.main<Main.java:46>
but when I typed javah myclass
Error:could not find class file for 'myclass'
Previously, when I typed ls in the my class directory, the class file was exist.
this is my snippet codes
package com.blablabla
public static native long myclass(long n);
Does anybody know how to solve this issue?
thanks

May be you should try giving fully qualified class name: -
javah com.blablabla.myclass

The class name starts with a lowercase character, try using uppercase, as is the mandated convention in Java. Additionally, try specifying the fully-qualified name (listing all the packages separated by .)

Get rid of the .class part of the argument. The argument is the fully qualified class name, not a file name. See the Javadoc.

Getting the fully qualified class names is a huge pain. You might take a look at the set of scripts I wrote to deal with this headache or just the script that deals with this particular problem if you want to write your own solution.

I opened the cmd window (Windows, obviously), and from the folder containing javah.exe, I typed
javah -classpath C:\my\project\path\PackageDirectory\packagename\build\tmp\kotlin-classes\debug com.company.package.MyKotlinClass
The file MyKotlinClass.class was in the folder
C:\my\project\path\PackageDirectory\packagename\build\tmp\kotlin-classes\debug\com\company\package\
Note, I used a mixture of specifying the class path and qualifying the class name.

Related

FIle in the same Package is not Found

I am using spring framework.
I have an interface And a class In the same package.
My interface is
package soundsystem;
public interface CompactDisc{
void play();
}
My class is
package soundsystem;
import org.springframework.stereotype.*;
#Component
public class Sgtpeppers implements CompactDisc{
private String title = "A Movie";
private String artist = "The Movie is Being Played";
public void play(){
System.out.println("The CD is Played \n"+title+"\n"+artist);
}
}
On Compilation it gives me this error
Sgtpeppers.java:5: error: cannot find symbol
public class Sgtpeppers implements CompactDisc{
^
symbol: class CompactDisc
1 error
The Interface is Compiled First and the .class file is also stored the soundsystem package.
I Think there is something wrong with the javac Command.
The command i used is
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
Is this because i changed the classpath?
1) As a general rule, to compile classes, don't perform the task from the directory of a specific package.
Instead, use the root of the application source code as base to run the javac/java command.
By following this simple rule :
2)To compile all classes located in a same package, just specify the package as "source files" :
javac soundsystem/*.java
3)To compile a specific class depending on another compiled class of your source code (in the same package or not), you don't need to specify "." (that represents the current directory) in the classpath as it is the default value.
Java documentation states indeed that :
The default class path is the current directory. Setting the CLASSPATH
variable or using the -classpath command-line option overrides that
default, so if you want to include the current directory in the search
path, then you must include a dot (.) in the new settings.
But if you explicitly set the classpath with another value, the default value is not more used.
And here you did it :
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
So you should add explicitly "." in the classpath too.
From the source code root, it would give on Windows :
javac -d . -cp ".;spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java
For Unix, separator char is :, so it would give :
javac -d . -cp ".:spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

Error: Could not find or load main class Java, because class file anme and class name are different?

I can not get some simple Java code to run in Eclipse. My code is as follows:
class DrinkAgeTest{
static int age =14;
public static void main(String[] args){
if (age >=21){
System.out.println("you may drink");
}
else {
System.out.println("you may not drink,come back in " + (21-age));
}
}
}
My class created in the source folder is named MathLearning. When I try to run my code I get the following error message:
Error: Could not find or load main class MathLearning
Is it because my class DrinkAgetest is different from the class file name, which is named MathLearning?If so how do I fix it. I couldn't find a rename button on the src right click menu.
which class have main that is must be public..
public class DrinkAgeTest{
}
How many source files do you have?
What is in them?
If it's just one - say "A.java" make sure it defines "public class A" in itself.
As said by others already, the class name must match the file name. Then go to
a command prompt and do "javac A.java". This will produce file A.class.
Then again from a command prompt do "java A". This will run your A.class file.
"javac" is the Java compiler
"java" is the JVM
Renaming the class file is not the way to do.
The class name and the name of the file containing the class have to be the same.
If you are using eclipse, it should fix it for you automatically.
To rename your class:
Right click on the class DrinkAgetest in your src folder, and click Refactor. Then click Rename...
If renaming the class doesn't fix the problem (it shouldn't fix it), then try what subash said:
change class DrinkAgeTest to public class DrinkAgeTest.
There could be other problems, possibly your compiler is looking for the wrong class altogether because you deleted it or something.

'Exception in thread "main" java.lang.NoClassDefFoundError' when running java program from command line

What am I doing wrong here:
class Helo {
// main: generate some simple output
public static void main (String[] args) {
System.out.println ("Hello, world."); // print one line
System.out.println ("How are you?"); // print another
}
}
When I go into terminal I do:
cd ~
javac Atempt2.java (//that's the file name)
java Atempt2
and then it gives me this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
So all in all this is what I do and what happens:
david-allenders-macbook-pro:~ davidallender$ cd ~
david-allenders-macbook-pro:~ davidallender$ javac Atempt2.java
david-allenders-macbook-pro:~ davidallender$ java Atempt2
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
david-allenders-macbook-pro:~ davidallender$
I am very new at this so please explain things in a very simple manner.
Thanks.
Its been awhile since I've done any java work but I'm pretty sure your class name needs to match your file name.
javac uses the class name to generate the output not the filename.
So it will generate a Helo.class classfile.
java will take a class name and call the main function in the corresponding class file, here Hello.class.
The ClassNotFoundError is thrown because javac never generated an Atemp2 classfile as there is no Atemp2 class in your source file.
Rename your Atempt2.java to Hello.java to get going, then:
javac Helo.java
java Helo
See here for more discussion and the reasoning.
change:
class Helo
to
class Atempt2
in your source file.
A .java file that declares a class must have the file name match the declared class name.
The filename must match the name of the public class defined in the file. In this case, you would either have to name the file "Helo.java" or renamed the class to Atempt2.
This is the very basic to start with java programming.Any program you write the name of the file must match with the public class of the program.
Here in your program public class of the file is Helo so your file name must be Helo.java.Here the compiler is able to compile but JVM will search for Helo.class file to run. As there is no Helo.class file you are getting runtime Exception Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
to complement josefx's answer.
The argument to the compiler (javac) is the name of the file or files to compile (as you did).
On the other side, the virtual machine (java) gets the name of the class whose main method is to be executed.
One option would be
javac Atempt2.java // the file name
java Helo // the class name
Normally it is a good idea to have the file named the same way as the class. For public class this is a must (checked by compiler).

Problems when trying to compile a Hello World with Eclipse

I'm having problems when trying to compile the following code:
I first tried compiling with this code:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
it works as it should. Now, if I try to replace the class name for any other name, it won't work anymore, as it seems to always look after HelloWorldApp. I made sure the file is being saved and so, I even reopened Eclipse. Still the same error. Maybe this is a common problem, with a small work-around?
Thanks
edit: I see what you guys mean, but why does it work when I have the file name as "Main.java" and a class name of "HelloWorldApp" ?
You need to rename your .java file to match the class name.
Eclipse will rename your .java file to match your class name automatically if you use its refactoring support. Right click on the class name, hover over Refactor, and select the Rename option. Now when you rename your class, Eclipse will automatically rename your .java file to match.
This is how Java works. Class must have the same name as the file. So the filename for the class MyApp, must be MyApp.java
Rename main as HelloWorldApp so that your app becomes HelloWorldApp.java.
When changing the name of your class use the REFACTOR option - If you try to edit the name manually yourself the Eclipse Project loses track of your objects.

Categories

Resources