Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 13 days ago.
Improve this question
In my command prompt (on Windows 10), I can run javac with any java file just fine. But, when I go to run the created class with "java [classname]" I get
Error: Could not find or load main class [class name]
Caused by: java.lang.ClassNotFoundException: [classname]
I used to be able to run just the command "java [classname]" and it ran AOK, but now it's not working and I need "java -cp . [classname]" instead. Why? Is there a way around this?
Here's a bit of a timeline when I started having trouble:
Had to isntall JUnit for my CS class, around where the issues started happening
Had to install some Java extensions with VS code.
The Java runtime shouldn't be impacted by installing JUnit or Java extensions in Visual Studio Code, however it's possible that any modifications to your classpath or environment variables contributed to the problem.
You can try telling the Java runtime to look for classes in the current directory by changing the CLASSPATH environment variable:
set CLASSPATH=.
I fixed it, in the style of Daniel's answer.
Turns out that when I installed JUnit, the instructions had me put CLASSPATH in my environment variables. Removing the CLASSPATH variable or changing it to "." let me run from my cmd again without specifying classpath, since I removed what was redirecting my classpath. Then all I had to do was close and reopen my terminal.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I got a new computer and I'm trying to get Notepad++ to run how it did on my old one. I got the NPPExec plugin. When I click Execute, it brings up the commands I want to enter. My goal here is to have the code save, compile, and run in the console of Notepad++. Currently I have what I thought used to work on my old laptop.
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
java $(NAME_PART)
Whenever I try and execute it though, I get
Current directory: C:\Directory
javac HelloWorld.java
; about to start a child process: "javac HelloWorld.java"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
java HelloWorld
; about to start a child process: "java HelloWorld"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
I've tried it on files I used to have, and just a basic Hello World
class HelloWorld
{
Public static void main(String args[])
{
System.out.println("Hello World");
}
}
I don't get what the problem is, and would love if someone would be able to help.
Edit: I am using Windows
After checking here: https://beginnersbook.com/2013/05/first-java-program/ and seeing the Set Path section, I reinstalled Java, did the set path command, although the file path ended at \Java and did not have that whole JDK bit in it. I'm not sure if that's part of the issue and Java is not installed properly.
Edit 2: Tried to Find where I can install JDK, and everything sent me to Oracle's website. Is this needed?
Edit 3: Well, I tried just executing the javac command through command prompt and got
'javac' is not recognized as an internal or external command, operable program or batch file.
So I'm pretty sure it's the issue with JDK not being in the folder. I went to check my old laptop, and it didn't have JDK in the folder either. It gives the same error when using the javac command, but for some reason the java command works fine to execute the file. Do I really have to create an Oracle account just to get JDK? When I went to sign up it wanted my work phone number, so it definitely didn't seem like a free thing.
Edit 4: I tried following the Oracle documentation on installing a jdk. I did the download from there, but following that, I didn't see the file I needed. In the Java folder, there is now the jdk-16 folder. In the bin folder there, there are a number of .exe files, but I don't know if any of them are the ones I'm supposed to run. Now that I've installed it though, I'm still getting an error with javac command, but the java command will run files I'd previously compiled.
Let me summarize the problem:
An exercise asked me to launch a java application using the Windows 10 Command Line.
It taught me on a video how to add the IntelliJ path to the Environment Variables, so that, whenever I typed java Main I would get my Main.java/class application back.
The first problem that I had stumbled upon was that, instead of using java Main, I'd have to type java Main.java. I thought that it was due to a recent build for the JDK (v14.02), so I let that one pass.
The second problem that I had encountered was that, when the exercise asked me to launch an application using the package directories, the command line would return the following error: Error: Could not find or load main class com.pluralsight.organized.Main. Caused by: java.lang.ClassNotFoundException: com.pluralsight.organized.Main.
Revert whatever damage you've done to the system.
Install the Oracle JDK.
Put the Oracle JDK bin folder on your PATH variable.
Compile your code using javac Main.java
Run your compiled code using java Main
for the ones in the packages, you need to be at the package root, or have a class path to it. For example, let's consider a class com.example.Main
The folder structure should be:
D:/SomePath/com/example/Main.class
Then you need to be at D:/SomePath/ to execute java com.example.Main.
Alternatively, you could be anywhere else and execute java -cp D:/SomePath com.example.Main.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a java program compiled with jdk13, and installed jdk13 on the server running it. If I run the program from the commandline, there are no problems, however when I call it from my python script with subprocess.Popen, I get this error message in stderr
has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 55.0
How can this happen through the python script and not when I run it manually in the commandline?
Your python script might be using a different environment where java means something different (id est, points to a different executable).
Try call directly you can find in your command line with which java.
How can this happen through the python script and not when I run it manually in the commandline?
Basically, the version of java that is used with depend on:
the full pathname that you use; e.g. /usr/bin/java or
the PATH environment variable if you are launching it with a simple command name; e.g. java.
We can't see what your python script is doing, but there are a number of ways that you could get different behavior.
The script could be using a different full pathname for java.
Python or your python script could be overriding PATH.
You could be launching your python script via a shell script that is setting PATH differently. Note that shell initialization is rather complicated, and typically it sets up PATH depending on what init files get run.
If you are using sudo, that overrides PATH.
The PATH setting in your command prompt could have been set "by hand".
And that is just some of the more likely possibilities ...
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a class with the main method. When i'm using the Windows command line to run the main method of the class, the compiling process with "javac" command works well (current location for the command line - directory with this class). File named "Card.class" gets created nearly to "Card.java" file. But command "java Card" returns error with the description "Could not find or load main class". In Intellij Idea this method within the class works completely well.
What i've tried to do to solve the problem (some solves were taken from StackOverflow topics):
1)Full reinstall of JDK and JRE.
2)Rewrote all my environment variables:
JAVA_HOME = C:\Program Files\Java\jdk1.8.0_144
JDK_HOME = %JAVA_HOME%
PATH = C:\Program Files\Java\jdk1.8.0_144\bin
CLASSPATH = %JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\jre\lib\tools.jar
3)I tried different variations for the "CLASSPATH" environment variable. Also, I manually wrote this: "java -classpath "$s" Card", where the value for "-classpath" parameter i copied from Intellij Idea command, that had been returned after running the program in the IDE.
4)Tried some variations for class name in the command (from just "java Card"
to "java package_name.Card" in the source project directory). All of these variations were recognized by the command line, returning the same error.
I'm thinking that the problem with the "classpath" variable, but all solutions i found are suggesting different values for this variable, and any of them had worked. Last updated value of the variable is mentioned before.
Here is my class (possible that the problem is here):
package cards;
public class Card {
//Constants, constructor, getters, setters, validation checking methods
public static void main(String[] args) {
// Just to see in the command line that the method was invoked
System.out.println("WORK");
// "assert" checks for constants values
}
}
Stacktrace for my error:
myPathToTheProject\src\cards>java Card
Error: Could not find or load main class Card
Let's set classpath correct before run. This work fine for me: java -cp ./ Card or java -cp . Card. The part: -cp . or -cp ./ is for setting classpath to current folder.
Open the CMD in the location where you have the Card.class
set CLASSPATH=""
java Card
I assume you don't have package deceleration on Card.java
Java - CPLEX
For a seminar at university I need to program a model with java.
Since it is a maximization problem I also need to involve CPLEX methods. Therefore I successfully installed CPLEX on my Mac OS X Version 10.5.8.
To combine CPLEX and Eclipse (I also tried it with Netbeans) I connected my current project with the CPLEX library (Properties>Library>Add Library).
Eclipse recognizes the library because the methods aren't underlinded. Additionally I committed the following argument to my run configurations:
-Djava.library.path=/Users/myname/Applications/IBM/ILOG/CPLEX_Studio126/cplex/bin/x86-64_osx
When I try to run an exemplary code (that is valid) I get the following error:
java.lang.UnsatisfiedLinkError: /Users/myname/Applications/IBM/ILOG/CPLEX_Studio126/cplex/bin/x86-64_osx/libcplex1260.jnilib: no suitable image found. Did find: /Users/myname/Applications/IBM/ILOG/CPLEX_Studio126/cplex/bin/x86-64_osx/libcplex1260.jnilib: unknown required load command 0x80000022Exception in thread "main" java.lang.UnsatisfiedLinkError: ilog.cplex.Cplex.CPXopenCPLEX([I)J
at ilog.cplex.Cplex.CPXopenCPLEX(Native Method)
at ilog.cplex.CplexI.init(CplexI.java:6594)
at ilog.cplex.CplexI.<init>(CplexI.java:629)
at ilog.cplex.IloCplex.<init>(IloCplex.java:11067)
at ilog.cplex.IloCplex.<init>(IloCplex.java:11082)
at Knapsack.buildModel(Knapsack.java:54)
at Knapsack.main(Knapsack.java:122)
java.library.path must point to the directory containing the CPLEX shared library
try invoking java with java -Djava.library.path=...
could you please be so kind to help me, i don't know any more what to do.
Thank you for an answer
I found this question when having the same problem. followed the advice from the internet and found that I was putting the -Djava.library.path argument in the wrong place... You should really double check not only the paths but also if you put your arguments in the correct place and correctly typed. It seems silly but happens :)
Anyway, in my case, I am using NetBeans, I went to:
Project Properties
Run
in the VM Options text field:
-Djava.library.path=/Applications/IBM/ILOG/CPLEX_Studio126/cplex/bin/x86-64_osx
I hope this will help you
Make sure you closely followed the steps described in 1 for setting up eclipse.
Make sure the environment variable DYLD_LIBRARY_PATH is set in the environment eclipse is running in. See 2. First check if DYLD_LIBRARY_PATH is set. Second try to execute your java program via terminal.