I'm trying to run a java file which is in the /lib/jarfile.jar jar file in the path "il/co/codeguru/corewars8086/CoreWarsEngine" with this command in linux:
java -cp lib/jarfile.jar il.co.codeguru.corewars8086.CoreWarsEngine
but I get this error message:
Error: Could not find or load main class il.co.codeguru.corewars8086.CoreWarsEngine
I read I little bit about classpathes in java but i still don't know what is wrong with what I did...
this is the content of CoreWarsEngine
package il.co.codeguru.corewars8086;
import il.co.codeguru.corewars8086.gui.CompetitionWindow;
import java.io.IOException;
public class CoreWarsEngine
{
public static void main (String args[]) throws IOException
{
CompetitionWindow c = new CompetitionWindow();
c.setVisible(true);
c.pack();
}
}
Make sure the jar file exists at the location you expect it to be and matches the jar file name in the command. This may seam silly, but java that I have, 11 AdoptOpenJDK does not complain that it did not find the jar file:
java -cp nonexistingfile.jar il.co.codeguru.corewars8086.CoreWarsEngine
Error: Could not find or load main class il.co.codeguru.corewars8086.CoreWarsEngine
Caused by: java.lang.ClassNotFoundException: il.co.codeguru.corewars8086.CoreWarsEngine
Second make sure the jar contains the class you are trying to start. I have stumbled on at least one corewars8086 download on the net that does not have the CoreWarsEngine class.
If you got the sources from GitHub/codeguru-il/corewars8086 then you need maven to build it. The resulting jar will be in target/corewars8086-4.0.0-SNAPSHOT-jar-with-dependencies.jar. Or if you build it a different way then you need to figure out where the result jar will be and what is the name.
I managed to run it from the sources of the above repo
Related
We have a java app that needs to use a public class form a JAR file. After much frustration with the main application, we have created a simple repo here to try to figure out what is going on.
The overly simple file that ends up in the JAR file is as follows:
package com.mystuff.helpers;
public class printStuff {
public void showMsg(String msg) {
System.out.println(msg);
}
}
We create the JAR file with this command:
jar cvf MyJavaHelpers.jar com
The folder structure is as follows (the printStuff.java file is in the helpers folder):
A listing of the JAR contents is as follows:
jar tf MyJavaHelpers.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mystuff/
com/mystuff/helpers/
com/mystuff/helpers/printStuff.java
com/mystuff/helpers/README.md
Finally, the program that we have to use this simple class is as follows:
package com.mystuff.testapp;
import com.mystuff.helpers.*;
// To build the JAR file
// jar cvf MyJavaHelpers.jar com
// To display the contents of the JAR file
// jar tf MyJavaHelpers.jar
public class testDriver {
public static void main(String[] args) {
System.out.println("Starting testDriver");
com.mystuff.helpers.printStuff ps = new com.mystuff.helpers.printStuff();
// testPrintStuff(ps);
// testPrintStuffAgain(ps);
}
/*
private static void testPrintStuffAgain(printStuff ps) {
ps.showMsg("This is a fine kettle of clams");
}
private static void testPrintStuff(printStuff ps) {
ps.showMsg("This is a fine kettle of fish");
}
*/
}
In VS Code (v 1.55.0) We have a Java Project to contain our TestDriver that looks like this:
Finally, the issue is that when we try to run the test driver, we get the message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
com.mystuff.helpers.printStuff cannot be resolved to a type
com.mystuff.helpers.printStuff cannot be resolved to a type
at com.mystuff.testapp.testDriver.main(testDriver.java:15)
We have tried the command Clean Java Language Server Workspace which seems to indicate that it works, but we cannot get past this error.
Based on what we have looked at, the JAR file appears to be in the correct place (It is in the lib folder of the main app).
The import com.mystuff,helpers.; line does not show as an error, so it seems to us that it is found, however, the actual import of the printStuff class fails. We have tried the fully qualified class name as well as relying on the import and only using the short name. They both fail.
We have seem some guidance about setting the classpath, but have not been able to find how to do that explicitly in VS Code.
Of course, if we do not have this little helper in a JAR file, but just as a side-by-side in the same project, it works just fine. The issue that started us down this journey is trying to use a public class from a pre-packaged JAR file.
Any and all help is greatly appreciated. Thanks in advance.
Before adding the jar to library, you may run the command java -jar printStuff.jar to test if it could be executed successfully.
The error occurs because the class must be called with its fully qualified name. To be clear, the name of this class is not printStuff, It's com.mystuff.helpers.printStuff, so the right command should be:
Turn to the folder;
Compile .java file: javac com\mystuff\helpers\printStuff.java
Generate .jar: jar cvfe printStuff.jar com.mystuff.helpers.printStuff .\
Then readd it to referenced libraries and see if the error goes away.
I know this question has been asked a lot, but I have tried a few suggestions and am still getting this error.
I am running the jar as follows:
java -jar MyJar-1.0.jar com.me.ldap.ActiveMain,
where my ActiveMain.java file looks like this:
package com.me.ldap;
public class ActiveMain {
public static void main(String[] args) throws Exception {
...
}
}
I have also tried simply java -jar MyJar-1.0.jar with the same Error: Could not find or load main class error. I've also looked into the class path option but I don't think that applies.
I am creating it in Intellij as a Maven project. Maven > Lifecycle > package.
In order for:
java -jar myfile.jar
to work, there must be a manifest file in the jar file that points to a main class.
In order for you to specify the main class on the command line, you need to specify a classpath, not a jar file. Like:
java -cp myfile.jar com.me.ldap.ActiveMain
You are conflating these two things. Either create a manifest that specifies your main class and use the -jar switch, or simply use the -cp switch and specify your main class on the command line.
Have you tried removing the signature files? I have seen this error pop up sometimes when due to signature files as documented in "Invalid signature file" when attempting to run a .jar and Generated JAR throws ClassNotFoundException for main class is also in the similar vein.
I need help getting a file from a file to use it.
The file is specified in the -cp option when running jar through the console.
run the jar using:
java -cp myjar.jar:dir1/dir2/myfile.txt com.company.Main
execution result:
Exception in thread "main" java.lang.NullPointerException
at com.company.Main.main(Main.java:11)
source code:
package com.company;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
ClassLoader classLoader = Main.class.getClassLoader();
InputStream resource = classLoader.getResourceAsStream("dir1/dir2/myfile.txt");
System.out.println(resource.toString());
}
}
project tree
-- сom
---- company
------ Main.java
How do I get that file from -cp?
Since you specify dir1/dir2/myfile.txt in the getResourceAsStream() call, you want the directory containing dir1 on the classpath, which would be the working directory, i.e. .:
java -cp myjar.jar:. com.company.Main
The classpath can only specify:
Directories
Jar files
No other type of file is supported.
It appears that your computer cannot find the file. Right click on the file and select explorer to see the path or if you are using intellij idea you will see an option to copy the path when you right click on it.
My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:
Client_1/src/filedownload/Client.java
RMI_interface/src/filedownload/Hello.java
The Client.java file is dependent on Hello.java. filedownload is the name of package.
When I compile using the following command, it works.
javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
But when I execute the class file in the Client_1/src folder using following command, it does not work.
java filedownload.Client
The error displayed is
Could not find or load main class
I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.
The code structure is
package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{
}
}
Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?
Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).
(And the questions themselves suggest various possible points of failure in your scenario so look at it.)
All your steps seems to be correct. You didn't share the Client.java code which has main method.
Make sure you follow this main method syntax:
public static void main(String[] args){
...
}
E.g. if you write main without args, it can't be found.
You need to put your classes in a separate folder, separated from your sources.
javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
(folder 'bin' must exist already)
And inside folder 'bin' execute command:
java filedownload.Client
For some reason, I can't get java to run my program. Whenever I try I get the message
"Error: Could not find or load main class Project"
In Command Prompt I type cd Documents since the file is in my Documents folder, type
javac Project.java
then
java Project
to try and run it but I get the above error message.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project
{
public static void main(String[] args)
{
Code and stuff
}
}
There's a fair bit of code that I left out but I think this is the part that's messed up. Let me know if you need to see the rest of the code and I'll edit this.
Change
java Project
to (assuming Project.class is in your current folder)
java -cp . Project
as it is, you aren't setting a class-path.
You have add the path of .class files in classpath during execution
Run following command:
java -classpath C:\Users\DELL\Documents Project