How to run multiple java class files from command prompt? - java

I have multiple java files in a single directory that are related to a single program (Main). I have been using the following in the command prompt,
javac -cp . game/textbased/*.java
java -cp . game/textbased/Main
All related files are in a package called game.textbased. The parent folder series is textbasedgame/src/game/textbased.
I am not sure what is going wrong, the java files seem to compile as nessessary, however, when I try to run I get the following:
Exception in thread "main" java.io.FileNotFoundException: items.dat (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at game.textbased.WonderLand.loadItems(WonderLand.java:310)
at game.textbased.WonderLand.initGame(WonderLand.java:63)
at game.textbased.WonderLand.<init>(WonderLand.java:24)
at game.textbased.Main.main(Main.java:6)
I am really not sure what is going wrong, and how to run the files from here. I have read other stack overflow inqueries of a similar nature, and generally they say to go from a higher directory but that has not been working for me. I tried going from outside src, or from within the textbased folder, but only going from within the src folder has given me this particular error.
Thanks!

Related

Java code not reading CSVs after being compiled

So I have a finished java project. It can read CSVs when I press run class on VSCODE. The App class is the main class. However when I compile the project and type "Java App" on the terminal it can't find the location of the CSV. I'm not sure why. This is the error I get
Exception in thread "main" java.io.FileNotFoundException: AIR-QUALITY-PROJECT/lib/1year.csv (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at DataHandler.readDataFromCSV(DataHandler.java:18)
at App.main(App.java:14)
The CSV is in lib and I'm running App from the src. Again this CSV runs fine when I run the class on VSCODE
I tried putting the 1year.csv on the root, in the src, everything!!! it doesn't work when I compile it
This how I am calling the path
private static final String PATH = "AIR-QUALITY-PROJECT/lib/1year.csv";
Again runs perfectly when I press run class on VSCODE
App.java is in SRC and 1year.csv is in the lib folder
You are using a relative path. That means relative to where the application runs.
If you want to have a reliable application you should use an absolut path, and the best would probably be if you configure or pass the path when running the application.
In such cases it is useful to log what the currentPath is while the program is running. You could do this e.g. like this
System.out.println("Current path is: " + new File("").getAbsolutePath());
All your relative paths must be relative to this path.
P.s. There are various methods to get the currentPath in java. This is just one of them.

Java runs in eclipse and will compile, but wont execute on cmd, but still runs in eclipse. How can I get it to execute in cmd?

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.

Given property file not found

so I'm trying to run my program and I keep getting the error when I run my main class it says that given property file isn't found, below are two images of the file location and the arguments I've putten in and the error that appears, I'm struggling to realize why the file isn't being located, any help?
Because Windows in its wisdom thinks that hiding extensions of known file types is a good ting. You files is called input_parameters.prp and inputs.in, I guess.
If you give those names it would probably find this files.
To be sure you can open a cmd or powershell windows and run the dir command in that folder to see the complete names.

NoClassDefFoundError when attempting to run from command line

Apologies in advance - I know there are many, many, many very similar questions but I wanted to ask something specific to my situation.
I have a bunch of java and jar files in the same directory. I am able to compile fine and thus end up with a number of class files in the same dir. But when I go to execute the program it gives a NoClassDefFoundError saying it can't find the specified class:
C:\Users\DB\Desktop\nextreports-integration-demo\src\ro\nextreports\integration>
java -cp ".;*.jar" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
I tried the same thing from a higher-level dir but it made no difference:
C:\Users\DB\Desktop\nextreports-integration-demo\src>java -cp ".\ro\nextreports\
integration\*.jar;.\ro\nextreports\integration" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
The package statement in the source file is:
package ro.nextreports.integration;
I have a feeling I'm overlooking something very elementary. Thanks in advance.
Edit: Thanks very much. It works with the following:
java -cp ".\ro\nextreports\integration\nextreports-engine-6.3.jar;.\ro\nextreports\integration\commons-jexl-2.1.1.jar;.\ro\nextreports\integration\commons-logging-1.1.1.jar;.\ro\nextreports\integration\derby-10.10.1.1.jar;.\ro\nextreports\integration\itext-2.1.7.jar;.\ro\nextreports\integration\itext-rtf-2.1.7.jar;.\ro\nextreports\integration\itextpdf-5.0.6.jar;.\ro\nextreports\integration\jcalendar-1.3.2.jar;.\ro\nextreports\integration\jcommon-1.0.15.jar;.\ro\nextreports\integration\jfreechart-1.0.12.jar;.\ro\nextreports\integration\jofc2-1.0.1.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\poi-3.7.jar;.\ro\nextreports\integration\winstone-lite-0.9.10.jar;.\ro\nextreports\integration\xstream-1.3.1.jar;" ro.nextreports.integration.SimpleDemo
But why can I not use wildcards for the *.jar files? For instance, the following leads to a NoClassDefFoundError for a class in any jar file I don't make explicit:
java -cp ".;.\ro\nextreports\integration\*.jar" ro.nextreports.integration.
SimpleDemo
Suppose this is your directory tree:
src/
ro/
nextreports/
integration/
SimpleDemo.class
From your package declaration, your compiled class should be in the integration subdiretory. Check that there really is a SimpleDemo.class in that directory. If that is correct, its classpath includes the contents of the src directory.
That means that, if you didn't have any JAR dependencies, you could run your application from the src directory like this:
java -cp . ro.nextreports.integration.SimpleDemo
You need to use the fully qualified class name.
Since you do have jars, you have to include them in the classpath as well. Suppose you have one JAR in the directory above src, and another in the current directory, you could use:
java -cp ../one.jar;another.jar;. ro.nextreports.integration.SimpleDemo
If you run it from another directory, it will still work if you review the classpath's relative directories or use absolute directories to describe your classpath. I am not sure but usually the current directory is always included in the classpath by the java executable.

Can you run a .class file from terminal that is outputted by an IDE

I am trying to run a file from command line. The file is a .class file and is apart of a larger project that I compiled in Netbeans. I navigated to the .class file and ran
java MyFile
And I got:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
Caused by: java.lang.ClassNotFoundException: PersonTest.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: PersonTest.class. Program will exit
Whats up with that? (I should mention that i'm running ubuntu)
You need to check this useful link java - the Java application launcher:
By default, the first non-option
argument is the name of the class to
be invoked. A fully-qualified class
name should be used
So, you have to write the full qualified name of the class (this includes the package name).
So, the right way to execute your command is this (from the root dir where your class files are stored):
> java my.package.MyFile
Also, make sure to include all the needed dependencies at the classpath (-cp) argument (check the referenced link).
UPDATE: to include a classpath setting example:
java -classpath C:\MyProject\classes;C:\MyProject\lib\utility.jar my.package.MyFile
With this, the java runtime will search for the classes at the C:\MyProject\classes directory, and at the C:\MyProject\lib\utility.jar JAR file. You'll need not only your class direct dependencies, but the dependencies needed by the referenced files (the whole tree).
The answer appears to be in this line:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
It means you didn't type:
java MyFile
as you said in your original post, you typed
java PersonTest.class
you should have typed
java PersonTest
Yes you can, they are compiled by a java compiler. If you have the right version of the jvm (often other versions work aswell) than it can be run. The information about your error is not enough to tell what went wrong.
Your probably in the wrong folder, mistyped the classname, used a class in your code that couldn't be found, etc.
Unless your class is entirely standalone (i.e. only references java.lang classes like String), you'll need to add other classes/JARs to the classpath when you invoke Java.
The NoClassDefFoundError (which usually states the name of the class by the way, and always includes a stacktrace) indicates that an external class that was available when your class was compiled, is not available on the classpath at runtime.
EDIT based on update:
You're invoking your process incorrectly. You don't need to append the .class suffix of the file - doing so makes Java look for a file class class in a subpackage.
(P.S. you said you ran java MyFile. That's a lie, you actually ran java PersonTest.class. If you'd noted that to start with, it would have made it much easier for people to answer the question!)
Just consider this example
say I already have a folder src and I wrote in my notepad
package test.oye;
class testclass {
static public void main (String [] args)
{
int a=3;
System.out.println(a);
}
}
then what go to src folder and you ,yourself create a folder named test and inside it oye . Then put your .java file in it . Then cd src/test/oye only(in Command prompt or terminal).From there itself
javac testclass.java
cd src
java test.oye.testclass
This will work for sure.
If you don’t want to put .java file there … then just compile your .java file and get the .class file .
Now create the test folder and then oye inside it ….and put .class file inside it ….
Now go back to src …and then type
java test.oye.testclass;
according to terminal ide, android requires classes in DEX format when running them.
Try:
dx --dex --output=practice.jar practice.class
Then run using this:
java -jar practice.jar practice

Categories

Resources