just started learning java, i'm looking to save a .java file (TextIO.java) into my working directory. (which file makes up my working directory exactly?)
and then run it through cmd so that Eclipse can use the class.
I tried doing that by saving (and running thru cmd) the file in the folder containing the programs (yah right) i have written already. But Eclipse still cant find the class.
Can i get some help with this?
thanks
You have to create an Eclipse Java project first. The you can copy the file from file system explorer window and paste it on the Eclipse Package Explorer tree. Eclipse projects compile all files in their source folders automatically by default.
The TextIO.java clas you mentioned has a javadoc saying: ... TextIO provides a set of static methods for reading and writing text... This means you use it as a toolkit rather than executing as an application. Given TextIO.java and you class TextIOUtilizer are both in the root of your classpath (source folder in Eclipse), here is how you call a TextIO method:
public class TextIOUtilizer {
/**
* #param args
*/
public static void main(String[] args) {
// code
TextIO.skipBlanks();
//code
}
}
Now you run TextIOUtilizer as a Java application - it has a main method. Hope this helps.
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.
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 currently don't have a working way to edit and run Java on my computer, so I'm trying to get Atom working with Java (I realize it's not a Java IDE and I'm not trying to make it one, I just want to be able to do some light Java work on my laptop). I've installed the script and instant-build packages for Atom and wrote the following test code in a file called "main.java' in my project folder:
class Main{
public static void main(String[] args) {
System.out.println("please");
}
}
When I try to run the code with cmd+i (I'm on a 2012 MacBook Pro) and get the following error message:Error: Could not find or load main class main.
I'd be happy to provide any further information; thanks for helping!
The huge problem of learning Java is that you need to launch the projects in a very strict way, and setting the classpath is always problematic. The solution lays in the following (pretty enigmatic) line of the "Script" documentation:
Project directory should be the source directory; subfolders imply packaging.
So, instead of opening a plain file, open the project (folder) with .java classes or define inside the file, the package to which your .java belong.
It's due to the fact that JDE needs to create a virtual target in form of .classess and single .java file definitely can't be launched as standalone file. I suppose that "Script" is not able to locate the source folder when you try to execute seperate .java file.
Before launching your .java files always "Add Project Folder..."
Please remember that it's not possible to have several folders opened if they don't belong to the same project. Such situation cause problems of locating the right classpath and in the end javac prompts the error.
You have to name your file with the first letter in upper case Main.java, since it must match the name of your class
I replicated the issue quite easily. If I created a new directory in Atom itself and then tried to run the code it didn't work as your error message came up for me as well. Error: Could not find or load main class main.
I used an existing directory and then created a file inside that folder in Atom and ran the same code it worked. I then copied and pasted that same file into the directory of my choice and it worked.
I have written two simple Java classes (one of them containing "main()", and the other called by "main()").
Class #1 (containing "main()"):
package daniel347x.outerjar;
import daniel347x.innerjar.Funky;
public class App
{
public static void main( String[] args )
{
Funky.foo();
}
}
Class #2 (called by "main()"):
package daniel347x.innerjar;
public class Funky
{
public static void foo()
{
System.out.println( "Funky!" );
}
}
The above classes appear in different project root folders, and use Maven as the build system (each project has its own POM). The pom.xml file for the main project includes the proper entry to add daniel347x.outerjar.App as the main class, and it properly includes the dependency on daniel347x.innerjar. Both projects build successfully into JAR files.
I use NetBeans to wrap these as Maven projects (in fact, I used NetBeans to create both projects). When I run the main project from within NetBeans, it runs successfully and I see Funky! as the output.
However, when I attempt to run the main class straight from the Windows command line (cmd.exe), passing the JAR file containing Funky on the command line's classpath, as such:
java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar" -jar "P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar"
... I receive the dreaded NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: daniel347x/innerjar/Funky
at daniel347x.outerjar.App.main(App.java:7)
I have carefully confirmed that, inside the innerjar JAR file noted above containing Funky, that the path structure is daniel347x\innerjar and that inside the innerjar folder is the Funky.class file - and this file looks correct within a HEX editor where I can see the ASCII strings representing the name of the class.
The fact that the class can't be found defies my understanding of Java, which I thought allows you to pass JAR files as a -classpath parameter and it will be able to find classes inside those JAR files.
This very basic point has me flummoxed - an answer that explains what I am doing wrong would be greatly appreciated.
The classpath is ignored when using the -jar option. A way to run your app would be java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar";"P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar" daniel347x.outerjar.App
Perhaps a better approach would be to add a manifest file to the Jar that specifies the class path of the dependent Jars by relative paths. Then..
java -jar "P:\_Dan\...\outerjar-1.0-SNAPSHOT.jar"
..should do it.
Double clicking the main Jar will also launch it. That is mostly useful for GUIs.
I'm not able to give this question an apt title so apology for that.
I am making a modularised application. I load various jar files at runtime and invoke particular method of a particular class (of the jar file) at run time.
The jar file has some supported file. Now my jar file uses another application , lets say abc which is located in the same directory where i have kept the jar file. When i run the jar file then
new File(".").getAbsolutePath()
gives the correct path (this is where abc is located) and program runs fine. But when i load this jar file dynamically and invoke method using reflection above code gives the path of the parent program and abc is not found at that path.
Now my question is how do i find the path in which my jar file exists when i'm running my jar file's code using reflection.
Please let me know if you need more explanation.
Try something like this:
public static void main(String[] args) {
System.out.println(StringUtils.class.getResource("StringUtils.class"));
}
(Note: StringUtils is present on my classpath as a Maven dependency at the time) This gives me:
jar:file:/home/******/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar!/org/apache/commons/lang3/StringUtils.class
Since the class is in a JAR file, it also gives me the location of the class file within the JAR.