如何运行不同目录中的java类文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25237345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run java class file which is in different directory?
提问by sathis
Directory path:
目录路径:
c:\home\test\src\com\bsoft\conc
I have my java program in src
folder and I have my class file in conc
folder.
I need to run my java program from home
folder.When I run I'm getting error:
我的java程序在src
文件夹中,我的类文件在conc
文件夹中。我需要从文件home
夹运行我的 java 程序。运行时出现错误:
could not find or load main class
采纳答案by sathis
In my program com.bsoft.concis a package name where my class file for the compiled program will be stored.If I have to run that from homefolder we have to specify java -classpath test\src com.bsoft.conc."class-file-name"
在我的程序com.bsoft.conc是一个包名,我编译程序的类文件将存储在其中。如果我必须从主文件夹运行它,我们必须指定 java -classpath test\src com.bsoft.conc。”类文件名”
This is because we need to tell the JVM where it has to look for class file.
这是因为我们需要告诉 JVM 它必须在哪里查找类文件。
so , we have to specify navigation to the src using "test\src"and then class file location "com.bsoft.conc.class-file-name"
所以,我们必须使用“test\src”然后类文件位置“com.bsoft.conc.class-file-name”指定到src的导航
If you have set environment variable in advanced settings then it will also be overriden if you specify classpath in cmd
如果您在高级设置中设置了环境变量,那么如果您在 cmd 中指定类路径,它也会被覆盖
回答by mtk
It's time for you to read on about classpath
( a way to tell java compiler where to look for the class file you intend to run ).
Basically there are two ways to set classpath
是时候继续阅读 about classpath
(一种告诉 java 编译器在何处查找要运行的类文件的方法)。基本上有两种方法可以设置类路径
- a environment variable CLASSPATH having ':' separate directories in unix and ';' separated directories in windows
-classpath
or-cp
command line arg tojavac
command
- 环境变量 CLASSPATH 具有 ':' 在 unix 和 ';' 中分开的目录 Windows 中的分隔目录
-classpath
或-cp
命令行 argjavac
命令
Refer and read the below links completely
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
完整参考并阅读以下链接
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
回答by arjun99
Set your class path for this java file:
为这个 java 文件设置你的类路径:
java -cp C:\hello\build\classes com.javahowto.test.HelloWorld
or using Environment variables and run it from any third location from that machine.
或使用环境变量并从该机器的任何第三个位置运行它。
回答by ducky duck
I had a similar problem where I was trying to run a Java program that calls a method in a class that is in another directory. I read this page and added the directory to my classpath, but I made the mistake of using '~' which in Bash means '/home/user/'.
我有一个类似的问题,我试图运行一个 Java 程序,该程序调用另一个目录中的类中的方法。我阅读了这个页面并将目录添加到我的类路径中,但是我错误地使用了 ' ~' 在 Bash 中的意思是 ' /home/user/'。
So this command did NOT work
所以这个命令不起作用
java -classpath ~/CurrentDirectory:OtherDirectory program
java -classpath ~/CurrentDirectory:OtherDirectory 程序
But this command did
但是这个命令确实
java -classpath /home/user/CurrentDirectory:OtherDirectory program
java -classpath /home/user/CurrentDirectory:OtherDirectory 程序
回答by parsecer
The answers here don't touch on the spaces problem.
这里的答案不涉及空间问题。
If your path has spaces, you must wrap it inside the quotes, otherwise an error would show up:
如果您的路径有空格,则必须将其包裹在引号内,否则会出现错误:
C:
Program Files
MyProject
src
testpackage
Test.java
target
classes
testpackage
Test.class
package testpackage;
public class Test {
public static void main(String[] args) {
System.out.println("Test");
}
}
java -cp "C:\Program Files\MyProject\target\classes" testpackage.Test
回答by M. Gopal
In Linux ( Simple scenario with out consider them as packages )
在 Linux 中(简单的场景,没有将它们视为包)
> parent
|
|- stdlib
| |
| -Library.java
|- Main.java
COMPILE FROM PARENT > javac -cp ./stdlib: Main.java
从父级编译 > javac -cp ./stdlib: Main.java
RUN FROM PARENT > java -cp ./stdlib: Main
从父级运行 > java -cp ./stdlib: Main