I'm on windows 10 + jdk1.8
Use used maven to create a project named UseNative, package name is mygroup, files are under:
src\main\java\mygroup\
I have UseNative.java:
package mygroup;
public class UseNative {
public native void greet(String name);
static {
System.loadLibrary("UseNative");
}
#SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println(System.getProperty("java.library.path"));
new UseNative().greet("me");
}
}
Then under powershell:
javac UseNative.java
OK, and then:
javah -classpath . UseNative
or
javah -classpath . mygroup.UseNative
Both says:
Error: cannot find class file for 'mygroup.UseNative'
I tried to remove the package mygroup line from java file, and then it works! But anyhow, I need this line to comply coding standard.
Where does it get wrong, add additional parameter or environment?
Thanks!
Related
I am trying to run the following method in Loader.java from the command line:
public static void method(String path, String word)
As seen above, I must pass in the variables path and word, and I want the command line to display the System.out.println()'s in the method.
What command can I run to do this?
Note: when I run the following commands,
javac *.java
jar -cvf Loader.jar Loader.class
java -cp ./Loader.jar Loader
I get the following error:
Caused by: java.lang.NoClassDefFoundError: path/to/Loader (wrong name: Loader)
What must I do to successfully run method from the command line?
Here is minimum reproducible version of Loader.java:
public class Loader {
public static void main(String[] args) {
method("my/path", "my_word");
}
public static void method(String path, String word) {
System.out.println("Output after doing something");
}
}
Just do the following:
javac Loader.java
java Loader
In fact, if you are you Java-11 or above, you don't even need to use the first command i.e. you can directly use the following command:
java Loader.java
However, if you want to create a jar file and execute the class from it, execute the steps given below:
mkdir demo
cd demo
Now create/place Loader.java in this folder. Then,
javac *.java
jar -cvf loader.jar .
java -cp loader.jar Loader
Note that I've used a new directory, demo to make it clear but it is not necessary. Another thing you should notice is the . at the end of jar command which specifies the current directory.
How to process command-line arguments?
String[] args parameter in main stores all the parameters from the command-line e.g. if you run the following program as java Loader my/path my_word from the command-line,
public class Loader {
public static void main(String[] args) {
if (args.length >= 2) {
method(args[0], args[1]);
} else {
System.out.println("Command line parameters are missing");
}
}
public static void method(String path, String word) {
System.out.println("Path: " + path);
System.out.println("Word: " + word);
}
}
the output will be
Path: my/path
Word: my_word
I'm executing the programs from command line and using packages in them.
my program file names are TestA.java and TestB.java.
I've executed below initially
javac TestA.java
No issues for the above and it generated the class file as well
for the following i'm observing the issue
javac TestB.java
output :
TestB.java:2: error: '.' expected
import TestA;
^
TestB.java:2: error: ';' expected
import TestA;
^
2 errors
and the TestA.java file is
package a.b;
class TestA {
public static void methodPublic(){
methodPrivate();
}
protected static void methodProtected(){
methodPrivate();
}
static void methodDefault(){
methodPrivate();
}
private static void methodPrivate(){}
}
TestB.java content is :
package a.b;
import TestA;
public class TestB {
public static void main(String args[]) {
TestA.methodPublic();
TestA.methodProtected();
TestA.methodDefault();
}
public static void methodPublic() {
}
protected static void methodProtected() {
}
static void methodDefault() {
}
private static void methodPrivate() {
}
}
I'm executing the javac by navigating to b folder where these two files exist.
I'm executing the javac by navigating to b folder where these two files exist.
You don't want to do that; the fully qualified class name of every class includes the package. They form a tree. Much like your filesystem. From the b folder move up two directories (to the folder containing a - e.g. cd ../.. or cd ..\.. on Windows). Then
javac -cp . a/b/TestA.java a/b/TestB.java
Also, you would normally want that to be written to a "binary" output folder. So
javac -cp . -d bin a/b/TestA.java a/b/TestB.java
Finally, you don't need to import TestA because it is in the same package as TestB. But, if you want to you need
import a.b.TestA;
import java.util.Scanner;
public class Twk {
public static void main(String[] Twk) {
System.out.println("Welcome to Twk");
Scanner enterName = new Scanner(System.in);
String name = enterName.next();
}
}
Why does it happen?
Twk.java and Twk.class are at the same place.
Need some helps, thanks.
Error: Could not find or load main class Twk
https://i.stack.imgur.com/NlcUu.png
Try with java -cp . Twk, this will probably let the java command "sees" the Twk.class file
You have to make sure public class exists in the source file.
Twk java and Twk class is the same program.
Twk java is the source file and Twk class is executable file of the source code generated by jdk compiler.
Do this if not yet:
public class Twk{
// your codes
}
This work:
C:\Users\tsoiwingkui>cd desktop
C:\Users\tsoiwingkui\Desktop>javac Twk.java
C:\Users\tsoiwingkui\Desktop>java Twk
My main folder is ABC inside it is 2 folders named classes and src, inside src is 2 folders named objectFile and testFile, inside objectFile is ABC.java while inside testFile is TestABC.java.(inside classes is the same but .class instead) now ABC contains
package objectFile;
public class ABC
private int something;
while TestABC.java contains
package testFile;
import objectFile.ABC;
public class TestABC
error says TestABC.java:2: error: package objectFile does not exist
import objectFile.ABC;
Are you specifying the sourcepath? This tells the compiler where to find the classes that it needs to import.
javac -sourcepath src -d classes src\testFile\TestABC.java
Note that this compiles not just TestABC.java, but ABC.java as well (because of your import statement).
You can then put the classes into an archive using the jar command:
jar cfe myJavaArchive.jar testFile/TestABC -C classes .
This will create a new jar with the filename myJavaArchive.jar and entrypoint testFile/TestABC made from all the files in the classes directory.
Because it is the entry point, TestABC must have a main method, e.g.
package testFile;
import objectFile.ABC;
public class TestABC {
public static void main(String[] args) {
ABC abc1 = new ABC(1);
ABC abc2 = new ABC(2);
System.out.println("abc1.i is " + abc1.getI());
System.out.println("abc2.i is " + abc2.getI());
}
}
and
package objectFile;
public class ABC {
private int i;
public ABC(int i) {
this.i = i;
}
public int getI() {
return i;
}
}
Then you can execute the code using the java -jar command:
java -jar myJavaArchive.jar
I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error
"package com.api does not exist".
Test.java
import com.api.APIUser;
public class Test{
APIUser ap = new APIUser();
ap .login();
public static void main(String[] args){
//to do
}
}
APIUser
package com.api
public class APIUser{
public string login(){
//to do
return string;
}
}
If any one have idea why I am getting this error.please suggest me solution.
Thanks in advance.
put a semicolon after the package com.api like as below
package com.api;
clean and build the project and run if any issue inform
You have multiple issues in your code.
You have no line termination present for the com.api import in the APIUser class;
You have a syntax error in your login method.
Below is the improved code:
import com.api.APIUser;
public class Test {
// APIUser ap = new APIUser(); // This call should be in the method body,
// there is no use to keep it at the class level
// ap.login(); // This call should be in method body
public static void main(String[] args) {
// TO DO
APIUser ap = new APIUser();
ap.login();
}
}
APIUser
package com.api; // added termination here
public class APIUser {
//access specifier should be public
public string login(){
//to do
//return string;//return some value from here, since string is not present this will lead to error
return "string";
}
}
Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.
You can use the code below to understand how to compile your class using classpath from command prompt.
javac -cp .;/lib/user.jar; -D com.api.Test.java