简体   繁体   English

如何从目录编译java程序?

[英]How to compile a java program from directory?

I'm learning java and I would like to know how to compile a java program from other directory. 我正在学习java,我想知道如何从其他目录编译java程序。

For example, my compiler is in my drive c:\\ and I want to compile my java program from drive e:\\ 例如,我的编译器在我的驱动器c:\\ ,我想从驱动器e:\\编译我的java程序

How should I do that? 我该怎么办? I'm getting this error, what does it mean? 我收到这个错误,这是什么意思? 替代文字

The current directory should be in the default CLASSPATH, but maybe it's not. 当前目录应该在默认的CLASSPATH中,但可能不是。 Try java -cp . Assignment 试试java -cp . Assignment java -cp . Assignment

It's been a while since I've done java, but it seems like the compiling isn't your problem. 我做了java已经有一段时间了,但似乎编译不是你的问题。 Since javac returns properly, it seems to be a problem with Assignment.java . 由于javac正确返回,因此Assignment.java似乎存在问题。 Does your Assignment class have a main method? 你的Assignment类有一个main方法吗?

Well the easy way is to set ur classpath variable. 好的方法是设置ur classpath变量。 Since from screen shot it seems ur using windows i suggest u right click the my computer nd select properties. 从屏幕截图来看,似乎你使用Windows我建议你右键单击我的电脑和选择属性。 Go to advance setting and click environment variable tab. 转到高级设置,然后单击环境变量选项卡。

Then a new window pops up which has System Variable at bottom. 然后会弹出一个新窗口,其中底部有System Variable。 Select new and create a variable JAVA_HOME = path where u installed java eg for me its c:\\java 选择new并创建一个变量JAVA_HOME = path,其中你安装了java,例如我的c:\\ java

Now once u add this search an existing variable path and choose edit. 现在,一旦您将此搜索添加到现有变量路径并选择编辑。 Now at the end append the following ;%JAVA_HOME%\\bin Now ur done u cn run java program frm any location on ya comp.... 现在最后添加以下内容;%JAVA_HOME%\\ bin现在你做了你的运行java程序frm在ya comp上的任何位置....

You are using package in your code...so it shows NoClassDefFoundError when you run you should create folder which contain your package name...compile that java file...and you can run that file from previous directory of that java file directory... 您在代码中使用了包...所以当您运行时它显示NoClassDefFoundError您应该创建包含您的包名称的文件夹...编译该java文件...并且您可以从该java文件目录的先前目录运行该文件...

For example your code is 例如你的代码是

package test;
class Assignment{
public static void main(String args[]){
System.out.println("Hai");
}
}

it saved on this path " E:\\java\\test " 它保存在这条路径上“ E:\\java\\test

compile this file and you can run this file from this path " E:\\java " 编译此文件,您可以从此路径“ E:\\java ”运行此文件

command to run this file 命令运行此文件

java test.Assignment

E:\java> java test.Assignment

Is there a package declaration at the top of your Assignment.java? 在Assignment.java的顶部是否有package声明? If so, remove it and recompile for a quick fix. 如果是这样,请将其删除并重新编译以进行快速修复。

To work with Java packages, you'll need a directory structure that matches the package declarations. 要使用Java包,您需要一个与包声明匹配的目录结构。

For example, say this is your Assignment.java: 例如,假设这是你的Assignment.java:

package myjava;

public class Assignment {
    public static void main(String[] args) {
    ....
    ....
}

You run this command to compile: 您运行此命令来编译:

E:\java>javac -d . Assignment.java

And you get myjava\\Assignment.class if all went well. 如果一切顺利,你得到myjava \\ Assignment.class。 The -d . -d . option means "place generated class files in the current directory". 选项意味着“将生成的类文件放在当前目录中”。 javac creates the package hierarchy as directories for you. javac为您创建包层次结构作为目录。

Now that your directories match your packages, this should work: 现在您的目录与您的包匹配,这应该工作:

E:\java>java myjava.Assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM