Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I was just trying to compile my java code then I did this in the cmd:
chdir C:\All_files (my directory)
javac Hello.java
then when I did this:
java Hello
오류: 기본 클래스 hello을(를) 찾거나 로드할 수 없습니다.
(Error: cannot find class hello)
then I did this:
javac myclass
then it worked.
How do I fix that?
Here is my code:
public class Hello
{
public static void main(String[] args) {
System.out.println("Hello Windows 10");
}
}
I want to know how do I make javac produce the right title for the compiled class.
What do you want to fix?
First you compiled your source file with javac Hello.java...
Then you tried to run it with java Hello...
However the command java requires a fully qualified class name. What you supplied (Hello) seems to me, like the name of he .class file, without extension.
When you tried with java myclass, it worked, because it is the fully qualified class name of your class...
See: java and javaw reference
Related
I can see that this question has been asked before, but I have not found previous answers useful - specifically my issue is following Princeton University's 'Algorithms' course on Coursera here and using their algs4.jar file to access ostensibly necessary utilities. I have never used Java before but this seemed like such a good course, I'm trying to muddle through rather than switching to a 'worse' course in a language I know. I have also found a thread specifically on this issue here and a Reddit question here, which were equally as useless as the other questions which had this problem, but not with the Princeton's algs4.jar file.
This is my code, I haven't bothered to finish it but it should run. Before anybody asks, I've commented out everything except the first if (StdIn.isEmpty()) check and it still gives me all the same compiler/interpreter errors, so this is nothing to do with it being half-finished - StdIn.isEmpty() and StdIn.readString() are supposed to be provided by algs4.jar:
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
class RandomWord
{
public static void main(String[] args)
{
if (StdIn.isEmpty())
{
System.out.println("Nothing to read.");
return;
}
String curr_selected_word;
double curr_probability = 0.0;
int i = 0;
while (!StdIn.isEmpty())
{
String current_string = StdIn.readString();
System.out.println("> " + current_string);
}
}
}
Following the advice of previous threads, I compile like this:
javac -cp "./algs4.jar" RandomWord.java
Which works fine. Running java RandomWord, I get this large messy error:
Exception in thread "main" java.lang.NoClassDefFoundError: edu/princeton/cs/algs4/StdIn
at RandomWord.main(RandomWord.java:13)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdIn
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
The accepted answer on the other question was to reference the .jar file explicitly, like this:
java -cp "C:\Coursera\Algorithms\Part 1\Java Solutions\HelloWorld;C:\Coursera\Algorithms\Part 1\Java Solutions\algs4.jar" RandomWord
Which gives me the exact same error. There is also a comment on the Reddit thread that uses the -classpath instead of -cp flag, which at least gives a different but stranger error:
>java -classpath ./algs4.jar RandomWord
Error: Could not find or load main class RandomWord
Caused by: java.lang.ClassNotFoundException: RandomWord
Putting quotes around the .jar filepath, or providing the absolute filepath, all produce the same error.
I Googled this and predictably it is caused by not having a main entry-point, but... see code above?
Lastly, the installer for the Coursera course apparently installs some Bash commands (see here) called javac-algs4 and java-algs4 which is supposed to sort out all this classpath nonsense for me. So I crack open Bash, and:
>javac-algs4 RandomWord.java
'javac-algs4' is not recognized as an internal or external command,
operable program or batch file.
I have restarted since running the installer. I would add these to my PATH manually, but I don't even know where they are; the C:\Program Files\LIFT-CS folder that their installer installs into only contains a pissing uninstaller. So I'm absolutely losing my rag and at my wits' end after spending over 7 hours just trying to start this course, not even getting stuck on the problems or the content. When will content creators learn we don't want your in-house IDE with no dark mode and libraries that do nothing but rename every function to camel-case?
Anyway, if anyone has encountered this or knows what I could do to fix it, help would be appreciated.
You can try this -
javac -cp .;<insert class path> RandomWord
I was facing the exact same issue, I ran javac -cp .;.lift/algs4.jar RandomWord and it works. (My compiled class was in the same directory as .lift)
This worked for me on a Mac. Copy algs4.jar to the root of your project, then:
javac -cp ".:./algs4.jar" RandomWord.java
java -cp ".:./algs4.jar" RandomWord
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a program in which the properties file is in the same path as the program I'd like to run.
The properties file name is initialized at the start of the program with:
public static final String DEFAULT_PROPERTIES_FILE = "defaultProperties.properties";
The program runs perfectly when running it in the directory it itself is located in with:
java -jar program.jar
When running the program from another directory with:
java -jar path/program.jar
The program will not find the properties file and returns a:
java.io.FileNotFoundException: defaultProperties.properties (No such file or directory)
FIXED
I came up with a solution. It is possible to call the path of the class and give the path to the configurations file.
StringBuilder stringBuilder = new StringBuilder( PolicyBoosterTool.class.getProtectionDomain().getCodeSource().getLocation().getPath() );
String pathToFolderOfJarFile = stringBuilder.substring( 0, stringBuilder.lastIndexOf( "/" ) + 1);
DEFAULT_PROPERTIES_FILE = pathToFolderOfJarFile + "defaultProperties.properties";
Sadly I can't mark the case as solved for myself. Any improvements to this are always welcome. Thank you for your help everyone!
You have to specify the complete path of the property files. If you don't specify the path, then it will try to find file in same directory from which you started the jar. Hence FileNotFoundException.
One simple solution is - define property file's path as command line argument.
One way, to keep your properties file at any arbitrary location and make your application read it, is using VM params
for example:
Pass a VM Param say prop.path
java -Dprop.path=path/to/your/properties/file -jar path/program.jar
Then you can read it with
System.getProperty("prop.path")
Hope this helps.
This question already has answers here:
Running java in package from command line
(5 answers)
Closed 8 years ago.
I have already come across this error. I couldn't figure out today also.
package com.example.cassandra;
public class test
{
public static void main(String[] a)
{
System.out.println("test");
}
}
This is my java file. My working directory is
com/example/cassandra
Compile command is
javac test.java
Changed working directory to parent directory of com
cd ../../..
Run command
java test
Says
could not find or load main class test
Any body please explain what's the problem here?
You will need to actually specify the package name:
java com.example.cassandra.test
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I made a project and named the class the same as the file name. Let say I saved the project and wanted to run it. How do I do this through emacs? I also have jdk installed.
I have a few hacks left over from when I was trying out Java.
This one is for the simplest programs consisting from a single file:
(defun java-eval-nofocus ()
"run current program (that requires no input)"
(interactive)
(let* ((source (file-name-nondirectory buffer-file-name))
(out (file-name-sans-extension source))
(class (concat out ".class")))
(save-buffer)
(shell-command (format "rm -f %s && javac %s" class source))
(if (file-exists-p class)
(shell-command (format "java %s" out) "*scratch*")
(progn
(set (make-local-variable 'compile-command)
(format "javac %s" source))
(command-execute 'compile)))))
This one is for an ant-controlled project:
(defun ant-compile ()
"Traveling up the path, find build.xml file and run compile"
(interactive)
(save-buffer)
(with-temp-buffer
(while (and (not (file-exists-p "build.xml"))
(not (equal "/" default-directory)))
(cd ".."))
(set (make-local-variable 'compile-command)
"ant -emacs")
(call-interactively 'compile)))
Some equivalent of these you can probably find in JDEE
I you can set it up (which I couldn't).
And here are the key bindings I have:
(define-key java-mode-map [C-f5] 'java-eval-nofocus)
(define-key java-mode-map [f5] 'ant-compile)
You'll have to have a version of emacs that has some feature to launch separate applications. From there, you pass the command to launch the JVM (e.g. java MyClass)
If you are developing a web application, your server may be able to dynamically load classes as they change -- it depends on many factors. If you are developing under that type of environment, then you only need to compile your Java code for the changes to be reflected on the server (assuming the server does dynamic class loading and it works for your development environment -- I've worked on many projects where it doesn't).
You should perhaps install and test out this:
http://www.emacswiki.org/emacs/JavaDevelopmentEnvironment
I'm having trouble running my very first piece of java. I was able to compile it and produce a .class file, but then I was unable to run it for some reason. It might have something to do with the directory path. The file name is "Simple" and I have this saved in a folder called "newfolder".
I was able to compile Simple.class by typing in "javac newfolder/Simple.java", but when I typed in "java newfolder/Simple", this message appeared:
java: exception in thread “main” java.lang.NoClassDefFoundError: newfolder/Simple (wrong name: Simple)
Here is the original code that I typed in:
// This is a simple Java program.
public class Simple
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
I think that the answer here is up my alley, but I've somehow been unable to get it to work for me, so any assistance would be greatly appreciated. Thanks in advance!
Try running the Java file from inside the newfolder/
cd newfolder/
java Simple
I suspect you want to use an IDE which sets up these things for you but you need
package newfolder;
at the start.