I created a simple java class which gets the name of the operating system. I also created a batch file to run the java class with the bundled JAVA 7. It seemed to work fine in my system. But when I copied this file and ran the script in another system it threw Error: Could not find or load main class OSNameGenerator .
At first I thought This may have to do something with the script and that it is not able to find the bundled java due to some issues in path. But even simply running java file only from the cmd also is not working.
I know this feels is so simple yet I'm not able to find what is creating the issue here.
Here is my java class
public class OSNameGenerator{
public static void main(String[] args){
System.out.println(System.getProperty("os.name"));
}
}
and This my batch script
"%~dp0\jre\bin\java.exe" OSNameGenerator
pause
And this is my file structure
Please help me to find out what is wrong with this.
This seems to be an issue with classpath.
I fixed it by changing the classopath to current directory using (.) operator in my scrpit.
Here is my updated script
.\jre\bin\java.exe -cp . OSNameGenerator
pause
Related
So I have a basic hello world set up in eclipse and I can compile it using cmd easily (I have set all the necessary paths), however when I then try to use the java command to execute the hello world, it always returns the same error:
Error: Could not find or load main class helloWorld
Caused by: java.lang.NoClassDefFoundError: net/codejava/helloWorld (wrong name: helloWorld)
This is the code used:
package net.codejava;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I am cd in the right directory (I think, I cd into the src directory and then into the package file stored in src) and am using Windows 10 with java 18.0.1 and JRE build 18.0.1+10-24
Any help would be greatly appreciated, as this is highly frustrating, when the code runs effortlessly on the eclipse console. Thanks.
Your file has a 'package' of net.codejava and a name of helloWorld, meaning, the full name of this class is net.codejava.helloWorld.
The java command, at least in the way you're using it, requires that you pass the full name, thus, you must run java net.codejava.helloWorld. Just java helloWorld simply isn't going to work.
But that's not all.
Java needs to then find the class file that contains the code for class net.codejava.helloWorld. It does this by first turning that full name into a path of sorts: net/codejava/helloWorld.class, and it will then scan each entry in the classpath for that. You can put directories and jar files on the classpath.
Thus, you have a directory on your system; let's call this directory X. X contains a directory named net, which contains a directory named codejava, which contains a file named helloWorld.class. If there is no such X (i.e. your class file is not in a dir named codejava for example), you're going to have to fix that by making these directories.
Then, X (and not the codejava dir!) needs to be on the classpath. Usually (it depends on how you configured things), 'the current dir' is by default on the classpath.
Given that your code is in, say, /home/PythonSux/workspace/learningjava/net/codejava/helloWorld.class, that means the dir that needs to be on the classpath is /home/PythonSux/workspace/learningjava. After all, if you, from there, look for net/codejava/helloWorld.class, you find the right file.
Therefore, either cd to that directory, or run java -cp /home/PythonSux/workspace/learningjava net.codejava.helloWorld
Note that this isn't usually how you actually run java apps. You either run them from your IDE, or you ask your build tool to run it, or you package your java app into a jar file and run that, etcetera.
I know this question has been asked loads of times before, but I'm a rookie programmer and despite trying many of the solutions on this site I still can't fix this issue. I'll be really thankful if you can take the time to figure out what I've done wrong.
Operating system: Windows 8
Java version: 1.8.0 update 25
The command prompt I'm using is the one that comes with Windows. (I'm presuming there are other types so I'm just making it clearer.) The code's a really basic one.
package com.thefinshark.intro;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome.");
}
}
So, first I changed the directory to C:\javawork, where Welcome.java is saved. I set the path to C:\Program Files\Java\jdk1.8.0_25\bin, then compiled the code. The compilation seemed fine, I found the Welcome.class file in the C:\javawork as well. The execution, however, kept returning "Could not find or load main class Welcome". I've tried C:\javawork>java Welcome and C:\javawork>java com.thefinshark.intro.Welcome, and loads of other variations. I've also changed the classpath to C:\ and C:\javawork but it still dosen't work. Someone answering a similar question suggested adding dt.jar and tools.jar to the classpath but no dice.
It'll be great if someone could help, and I'll be happy to help pass on the information to the others who have problems like this as well. (As I'm typing this I'm looking at a whole long list of similar questions.)
The directory structure must match the package name of your source file. So, if your class is in the package com.thefinshark.intro, then your source file must be in a directory com\thefinshark\intro.
So, for example, you should save your source file as C:\javawork\com\thefinshark\intro\Welcome.java, and then compile and run it from the directory C:\javawork:
C:\javawork> javac com\thefinshark\intro\Welcome.java
C:\javawork> java com.thefinshark.intro.Welcome
Note: The javac command expects a filename of the source file you are compiling (com\thefinshark\intro\Welcome.java), and the java command expects a fully-qualified class name (com.thefinshark.intro.Welcome).
See Lesson: Packages for more details on how to work with packages.
I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java. tests is the package I created in Eclipse and src is a folder that Eclipse made under Introduction (which is the project name).
In my environment variables, I have the following relevant variable:
JAVA_HOME C:\Program Files (x86)\Java\jdk1.7.0_40\bin
Under system variables I have the following:
CLASSPATH %JAVA_HOME%
I go to my cmd and cd into the tests directory (cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests). Then I compile using javac Test.java. This seems to work as I then have a Test.class file under the same directory. Now I want to run the file, I type java Test and I get the error, "could not find or load main class". I've tried a variety of things including appending .class and .java to the end but I keep getting the error. I looked at some answers and docs and I managed to get it to work if I cd into:
cd C:\Users\MYNAME\Documents\Java\Introduction\src (i.e, get out of the package)
and then run:
java -cp . tests.Test
So that seems to temporarily set the class path to the current directory, and run Test from the package tests. However, I want to simply be able to type java Test. I know it's possible as I used to be able to do it, but now for some reason I cannot (I must have changed something along the way...).
Any help is appreciated.
However, I want to simply be able to type java Test
That will only work if Test is in the default package - it's as simple as that. You need to pass the java executable the fully-qualified name of the class you want to launch. There's no way round that.
Of course, you could create your own launcher which looks in the current directory for class files, finds out the fully-qualified name of the classes within those files, and launches java providing the full name and probably specifying an appropriate classpath... but that seems like a lot of hassle compared with just including the package name in the command.
You could be making the same mistake I made. So, try the following.
Here is my code for your reference.
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Once you saved this as "C:\JavaStudy\ClassA.java", try the following.
c:\JavaStudy>javac ClassA.java
c:\JavaStudy>java A.class
Error: Could not find or load main class A.class
c:\JavaStudy>java A
Hello world
c:\JavaStudy>
Note: You don't need to use " java.exe -cp . " if you have class file in the same directory from where you are executing.
I have been searching the web trying to find the answer to my question, but everywhere I look seems to have too complex of a solution for a beginner like me. I have been working on this project, and just now realized that I should've made a package, or something like that. The thing is though, my program was working fine until I started dabbling with it, and now it won't work at all. I am getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort. class
Caused by: java.lang.ClassNotFoundException: BubbleSort.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:672)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at ``java.lang.ClassLoader.loadClass(ClassLoader.java:638)
Could not find the main class: BubbleSort.class. Program will exit.
Here's how my "path" looks, if I am not mistaken. I am connected to my school's Z: drive through a remote connection, and from there I have a folder called myFirstname_Lastname_A4,
which then leads me to another folder called sortingzz which I believe is supposed to have only my source files, but it also ended up with my class files in there whenever I compiled. So here's what I am doing to compile.
cd myFirstname_Lastname_A4/sortingzz
javac *.java (Works fine, this is where I end up with my Class files inside of my sortingzz folder)
java * (This is where I get the error)
I am pretty sure I am just trying to run the program wrong. Before I started messing around with stuff I wasn't ready for, I used to just run the file with my main function in it, like this
javac SortingImplementation.java
java SortingImplementation
And that for the most part worked fine, but I started having trouble calling certain classes from other classes, so thats when I found out I was suppose to do the packaging and importing stuff.
In case that is the issue, I have on the top line of every source file:
package sortingzz;
and I am importing like this:
import sortingzz.*;
This is correct, right?
UPDATE:
I decided to give up on class pathing and trying to package everything, because as usual, I am getting responses that are way over my head, and to be honest I don't think it is necessary.
After removing package and importing from everything, and once again compiling using javac *., it actually compiles this time. However whenever I try to run my class with the main in it, SortingImplementation, it tells me that
Could not find the main class: SortingImplementation. Program will exit.
I don't get it, I am looking at the SortingImplementation.class right now, with all the other classes and java files, so I am not sure what it's trying to do.
javac *.java is fine. This will compile your files. However, you only need to run the file with your main method in it: java MainClass
You say that you are using packages to organize the classes. In this case you need to set the class path using the -cp flag.
javac -cp /path/to/parent/of/package classname.java
and
java classname
Also, your main class should be declared public and should have a main()
NoClassDefFoundError occures when a class was recognised in compile time but was not available during runtime.
So the JVM can't find your class in the classpath.
using -cp flag to specify where your package is should work.
the commanc javac *.java compiles all found java files to corresponding .class files. If you all your classfiles are in the same folder, which they should, you just run your regular java SortingImplementation command.
java * would, a bit depending on your OS, yield in an undesired command. For instance, on Linux it would be expanded by the OS to java SortingImplementation.java SortingImplementation.class BubbleSort. The last one is a directory, which ofcourse is not an executable class.
I'm able to run a program from the commmand line by typing "java main" where main.java and main.class are in the same directory as well as any related classes. This runs fine. When I try to run the same program in Eclipse I get Unsatisfied link errors. I think this is related to the JVM being used. I think that the command line java call is using a different JVM then eclipse. How can you specify which JVM java uses on the command line?
I'm getting an UnsatisfiedLinkError when I run a program in Eclipse that uses native libraries.
This isn't a typical "cannot find...." link error I believe it has actually found the file but there is some other problem.
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.me.this.MyClass.MyMethod(Ljava/lang/String;)I
You can see that if it just could not find the shared library it would say something like:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no MySharedFile.so in java.library.path
So I believe it is finding the file.
Some other issues that are relavent are the fact that if i run the program from the command line instead of in eclipse it finds the .so and runs the program perfectly. Also I had this program running before in a different Eclipse that was using an older 1.6 JVM. I've tried to use that in this eclipse but it hasn't helped.
Is this a problem finding the .so shared file? Or something completely different like I'm using the wrong JVM. I used strace on the java command line program and it appears it's using the new 1.7jdk the same one I'm using now in Eclipse and it will not work.
The .so is in /usr/lib64 and I've also created a -Djava.library.path=... entry in the vm arguments for the run configuration just in case.
I added these try catch around the load:
static
{
try{
System.loadLibrary("MyAwesomeLibrary");
System.out.println("MyAwesomeLibrary library loaded \n");
}
catch(UnsatisfiedLinkError e){
System.out.println("Did not load library");
e.printStackTrace();
}
}
And I get:
MyAwesomeLibrary library loaded
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.me.this.MyClass.MyMethod(Ljava/lang/String;)I
at com.me.this.MyClass.MyMethod(Native Method)
at com.me.this.Main.main(Main.java:8)
It's being called froma main class that looks like this:
public class Main
{
public static void main( String[] args )
{
ClassThatContainsLoadedLIbrary x = new ClassThatContainsLoadedLibrary();
int y = x.Ping( "thisaddress" );
So it appears that it's loading...at least it's getting to the print statement without link errors. the UnsatisfiedLink errors when it actually triest to use the library.
I've been working on this problem for weeks so would really appreciate it if some one had some insight into this. Thanks.
Look at this: http://docs.oracle.com/javase/7/docs/api/java/lang/UnsatisfiedLinkError.html.
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
The problem is not with the JVM per se, but with the JVM being unable to find your native libraries. You need to specify the path where the native libraries are stored. This can be done by adding the following as an argument to your JVM in eclipse:
-Djava.library.path=...
Here, take a look at this: http://mindprod.com/jgloss/runerrormessages.html#UNSATISFIEDLINKERROR
If you get the error after the class containing the native method is safely loaded, when you invoke a native method, make sure you generated your *.h file with the fully qualified javah.exe -jni -o mouse.h com.mindprod.mouse.Mouse and not simply javah Mouse
If you get the error after the class containing the native method is safely loaded, when you invoke a native method, check that the *.cpp method signatures exactly match those in the *.h file. You should see method names like this: Java_com_mindprod_mouse_Mouse_GetMousePosition that start with the word Java and contain the package, class and method name all strung together. Make sure you remembered to implement all the methods.
You need to regenerate the *.h and recompile the *.c file if you change the package name.
You should probably go through this list to make sure you're doing all the things correctly.
You should use java in your console to find options for java.
java -version:1.6 MyClass
Right click your project ----> Java Build Path ---> Libraries tab.
Then select JRE System library , and click Edit to bring the following screen to configure