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
Related
When I import a package to my MyLib class (which requires -cp to javac) I can no longer compile my MyMain class.
MyMain.java:
class MyMain
{
public static void main (String [] args)
{
MyLib.do_stuff ();
}
}
MyLib.java:
import com.google.gson.JsonElement;
class MyLib
{
public static void do_stuff ()
{
System.out.println ("Hello.");
}
}
When I javac MyLib.java I have do do it like this
javac -cp GSON_JAR_PATH MyLib.java
That works but if I
javac MyMain.java
I get
./MyLib.java:1: error: package com.google.gson does not exist
import com.google.gson.JsonElement;
but if I add -cp to the compilation command
javac -cp GSON_JAR_PATH MyMain.java
I get
MyMain.java:5: error: cannot find symbol
MyLib.do_stuff ();
^
symbol: variable MyLib
location: class MyMain
Use "-cp path1:path2" - colon separated. (semicolon works on windows) (the parameter to cp is quoted....
javac -cp path1:path2 //or ; for windows.
Note 1 - setting -cp overrides any existing CLASSPATH environment or default
path setting.
Note 2 - if no CLASSPATH setting then default is '.' - until the -cp overrides that.
So in the posted case - the "." was set for path (either CLASSPATH or default) up until the -cp was used which overrode that default - so it needs to be added back in.
So I typed the very first example of Deitel&Deitel's How to Java book which is
public class Welcome1
{
// main method begins execution of Java application
public static void main( String[] args )
{
System.out.println( "Welcome to Java Programming!" );
} // end method main
} // end class Welcome1
Then I saved the file as test.java and went to its directory in cmd and typed Java test.java and I got the error Error: Could not find or load main class test.java.
What am I doing wrong?
You have two mistakes. Your class must match the file name. So move "test.java" to "Welcome1.java". Then you must compile it before you can run it.
javac -cp . Welcome1.java
Then
java -cp . Welcome1
First you have to compile the class with javac
javac Welcome1.java
Then you can call the class Welcome1
java -cp . Welcome1
The file name needs to be Welcome1.java. Class names and file names need to match.
Edit: And as others have mentioned you have to actually compile your code with the javac command before trying to run it with java.
Your class and Java file name must be the same. Like if you were to rename Welcome1 to test it would compile or rename the file to Welcome1.java it would compile.
I have a package LMath with a class LMatrix. LMatrix has a method public LMatrix getInverse() that throws LDimensionException.
The first line in both of these files is:
package com.kavricious.LMath;
Compiling this class in jGrasp results in no problem, but if I enter
PS C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
in Windows PowerShell, the stack trace reads:
LMatrix.java:70: error: cannot find symbol
public LMatrix getInverse() throws LDimensionException{
^
symbol: class LDimensionException
location: class LMatrix
how do I tell javac to recognize members as in the same package?
C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
That should be
C:\programming\java\javaprojects> javac com\kavricious\LMath\LMatrix.java
And similarly for all other Java files: compile from the root of the package hierarchy, and name the entire path to the .java file. Then the object files will be put in the right place, and found, and the ither .java files will be compiled as necessary.
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.
I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance
Here's a concrete example of what does work, so you can compare your own situation.
Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result:
I'm MainClass
How are you building your jar file? Is the code in the appropriate package?
Does theMainClassName class have the following package line at the top:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. So if you had something like:
org/jc/tests/TestClass.class
its source file would have to look like this:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar?
Of course, it supposes the manifest points to the right class...
You should give the exact message you got, it might shed more light.
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file.
Usually this happens when a dependent class (static member) is not found - like this, using log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described.
Static constructors are another possible reason:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. I'd jar tf the jar and make sure the class is listed. I'd also double-check that the class is public and the method is "public static void main(String[] arg)".