Unable to run Java program - java

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.

Related

Exception in thread "main" java.lang.NoClassDefFoundError - Coursera Princeton Algorithm Course

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

How to fix "Error: Can not find or load the main class"?

I'm trying to execute a simple java code.
the code worked fine using comand line : javac Go.java and java Go, but in Spring Tool Suite 3, the code did not work and I got the error below.
Note that I'm using jdk1.8.0_211
package com.test;
public class Go {
public static void main(String[] args) {
System.out.println("test");
}
}
I expect print test, but I get this error :
Error: Can not find or load the main class.
the cause of the problem is that I'm not the owner of the Laptop, it is managed by the administrator of the company where I work. I noticed that when I change the version of JAVA, the code runs correctly, but after restarting the Laptop, I get the same error again, so the cause was that every restart of the laptop, it updates java version, I fixed my PC to not updating java version again and it works

How to use Eclipse and Java for Mac 10.8.1

When I try to do a simple "Hello World" java application I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/mainarg*
Caused by: java.lang.ClassNotFoundException: first.Skeleton
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I am running on Mac OS 10.8.1 and am running Java 1.6. I have checked my run configurations to make sure the main argument can be found. For the sake of simplicity I have also tried to use the default JRE of java 1.6 that is included with mac. The build configuration I have used is also correct and pointed to the right place.
When I followed this tutorial online, and executed the commands over the terminal I get the same error. What am I missing? I don't think it is an Eclipse problem anymore, but something bigger. Any advice would be greatly appreciated.
Here is the code I tried to run:
public class Test{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints Hello World
}
}
I put the code on my Desktop for simplicity. I went to /usr/tyler/desktop then made the script an executable with "javac Test.java", and when I tried to run it with "java Test.java" I then got the error.
Here are the other tutorials I have followed:
-Java Compilation error on Mac
-HelloWorld with Eclipse on Mac
-Along with many others from SO and elsewhere
EDIT:
Thank you for the help. The terminal issue has been resolved. However, if anyone has any advice on what could be going wrong with Eclipse, that would be appreciated.
You need to run ...
"java Test"
You cannot execute the java code, you need to execute the compiled class file. The java executable will look for a .class file with the name Test.
execute or run
java Test
instead of
java Test.java

NoClassDefFoundError on simple jar file

public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.

Exception in thread "main" java.lang.NoClassDefFoundError: DiServer <wrong name: ds/DiServer>

This is one of those terribly embarrassing questions I'm afraid.
I have a program in Eclipse:
package ds;
public class DiServer {
public static void main(String[] args) {
int foo = 0;
int bar = 0;
/*bla*/
}
}
Simple right? This works completely fine when run in Eclipse.
I want to run this from command line. I have copied bin Folder, with the ds folder inside it and DiServer.class in ds, and .classpath
I have put these into a separate folder, C:\My Documents\DiTest, opened command prompt, gone to C:\My Documents\DiTest\ds\ and typed java DiServer
The error I get is Exception in thread "main" java.lang.NoClassDefFoundError: DiServer <wrong name:ds/DiServer> ... Could not find the main class: DiServer. Program will exit.
I have tried java -classpath . DiServer, java -classpath ../.. DiServer, moving .classpath to the ds folder, but I can't seem to get round this. I'm 99% sure it's a classpath problem but I can't work out how to fix it.
I would greatly appreciate any help as always, and the customary offer of a pint always stands.
Thanks very much in advance,
M
You class full name is ds.DiServer, not DiServer. From C:\My Documents\DiTest:
java -cp . ds.DiServer
And voilà.
goto C:\My Documents\DiTest\ds\
javac DiServer.java
goto C:\My Documents\DiTest\
java ds.DiServer
Also See
packages

Categories