Why isn't 'java Class' working like normal from the current directory?
I have a simple program that works fine in a previous environment. I've moved it to a new one with the same OS and Java versions, but have encountered a problem executing the class files.
Never have I had a problem running a java class in the same directory but when I try java Class
I get the error could not find or load main class
.
I've already looked over all the suggestions, one of the most popular being to add the classpath and run it as java -cp . Class
. However this has never been necessary in all the environments I've worked in, including the previous one which was identical to this. Why would this happen and how can I fix it?
Note: In case you wonder, java -cp . Class
works for a small test program with no dependencies, but results in a JNI error which has no clear resolution, related to a jar file that is both on the bashrc classpath and the current directory (when specifying -cp .
). Regardless, I just want to execute classes like normal.
UPDATE
I'm certain this is an environment issue, but here is the code. I am calling it as java Test
, NOT java test
or java Test.class
.
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
16.04 command-line java openjdk
add a comment |
I have a simple program that works fine in a previous environment. I've moved it to a new one with the same OS and Java versions, but have encountered a problem executing the class files.
Never have I had a problem running a java class in the same directory but when I try java Class
I get the error could not find or load main class
.
I've already looked over all the suggestions, one of the most popular being to add the classpath and run it as java -cp . Class
. However this has never been necessary in all the environments I've worked in, including the previous one which was identical to this. Why would this happen and how can I fix it?
Note: In case you wonder, java -cp . Class
works for a small test program with no dependencies, but results in a JNI error which has no clear resolution, related to a jar file that is both on the bashrc classpath and the current directory (when specifying -cp .
). Regardless, I just want to execute classes like normal.
UPDATE
I'm certain this is an environment issue, but here is the code. I am calling it as java Test
, NOT java test
or java Test.class
.
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
16.04 command-line java openjdk
In your previous environment, did you have aCLASSPATH
environment variable set?
– glenn jackman
Jan 1 at 1:41
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17
add a comment |
I have a simple program that works fine in a previous environment. I've moved it to a new one with the same OS and Java versions, but have encountered a problem executing the class files.
Never have I had a problem running a java class in the same directory but when I try java Class
I get the error could not find or load main class
.
I've already looked over all the suggestions, one of the most popular being to add the classpath and run it as java -cp . Class
. However this has never been necessary in all the environments I've worked in, including the previous one which was identical to this. Why would this happen and how can I fix it?
Note: In case you wonder, java -cp . Class
works for a small test program with no dependencies, but results in a JNI error which has no clear resolution, related to a jar file that is both on the bashrc classpath and the current directory (when specifying -cp .
). Regardless, I just want to execute classes like normal.
UPDATE
I'm certain this is an environment issue, but here is the code. I am calling it as java Test
, NOT java test
or java Test.class
.
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
16.04 command-line java openjdk
I have a simple program that works fine in a previous environment. I've moved it to a new one with the same OS and Java versions, but have encountered a problem executing the class files.
Never have I had a problem running a java class in the same directory but when I try java Class
I get the error could not find or load main class
.
I've already looked over all the suggestions, one of the most popular being to add the classpath and run it as java -cp . Class
. However this has never been necessary in all the environments I've worked in, including the previous one which was identical to this. Why would this happen and how can I fix it?
Note: In case you wonder, java -cp . Class
works for a small test program with no dependencies, but results in a JNI error which has no clear resolution, related to a jar file that is both on the bashrc classpath and the current directory (when specifying -cp .
). Regardless, I just want to execute classes like normal.
UPDATE
I'm certain this is an environment issue, but here is the code. I am calling it as java Test
, NOT java test
or java Test.class
.
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
16.04 command-line java openjdk
16.04 command-line java openjdk
edited Jan 1 at 1:43
user58446
asked Dec 31 '18 at 23:57
user58446user58446
1083
1083
In your previous environment, did you have aCLASSPATH
environment variable set?
– glenn jackman
Jan 1 at 1:41
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17
add a comment |
In your previous environment, did you have aCLASSPATH
environment variable set?
– glenn jackman
Jan 1 at 1:41
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17
In your previous environment, did you have a
CLASSPATH
environment variable set?– glenn jackman
Jan 1 at 1:41
In your previous environment, did you have a
CLASSPATH
environment variable set?– glenn jackman
Jan 1 at 1:41
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17
add a comment |
1 Answer
1
active
oldest
votes
You need to compile your Java source code with javac Test.java
which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test
as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.
$ cat Test.java
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java
$ ls
Test.class Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!
As you can see, the last command has the directory containing the Test executable file after the -cp
classpath option, not the Test file itself as you wrote in your question.
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1105927%2fwhy-isnt-java-class-working-like-normal-from-the-current-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to compile your Java source code with javac Test.java
which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test
as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.
$ cat Test.java
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java
$ ls
Test.class Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!
As you can see, the last command has the directory containing the Test executable file after the -cp
classpath option, not the Test file itself as you wrote in your question.
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
add a comment |
You need to compile your Java source code with javac Test.java
which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test
as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.
$ cat Test.java
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java
$ ls
Test.class Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!
As you can see, the last command has the directory containing the Test executable file after the -cp
classpath option, not the Test file itself as you wrote in your question.
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
add a comment |
You need to compile your Java source code with javac Test.java
which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test
as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.
$ cat Test.java
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java
$ ls
Test.class Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!
As you can see, the last command has the directory containing the Test executable file after the -cp
classpath option, not the Test file itself as you wrote in your question.
You need to compile your Java source code with javac Test.java
which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test
as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.
$ cat Test.java
class Test {
public static void main(String args) {
System.out.println("Test complete!");
}
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java
$ ls
Test.class Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!
As you can see, the last command has the directory containing the Test executable file after the -cp
classpath option, not the Test file itself as you wrote in your question.
edited Jan 1 at 7:45
answered Jan 1 at 2:02
karelkarel
58.6k13128147
58.6k13128147
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
add a comment |
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself.
– user58446
Jan 1 at 6:50
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands.
– karel
Jan 1 at 7:41
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1105927%2fwhy-isnt-java-class-working-like-normal-from-the-current-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your previous environment, did you have a
CLASSPATH
environment variable set?– glenn jackman
Jan 1 at 1:41
@glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file.
– user58446
Jan 1 at 2:00
You might want to peruse some of the Related questions you'll find displayed in the right-hand column.
– glenn jackman
Jan 1 at 3:17