如何从现有 Java 项目中提取主类?

发布于 2024-12-28 18:05:57 字数 373 浏览 5 评论 0原文

我有一个大型 Struts1 Web 项目,也使用 Hibernate。

我们的包之一是:

com.company.examples.users

另一个是:

com.company.examples.orders

每个包中都有一个用于测试的 main.java 文件(实际上是从 Netbeans 中运行该文件)。

因此,我可以右键单击主文件并让它运行,输出将显示在日志窗口中。那很好。但我希望能够将它们提取到实际的 JAR 文件中,以便它们可以单独运行。

这些文件还依赖于整个更大的项目。很多包裹等等,

我该怎么办?

I have a large Struts1 web project that also uses Hibernate.

One of our packages is:

com.company.examples.users.

Another is:

com.company.examples.orders.

Within each of those packages is a main.java file that is used for testing (actually running the file from within Netbeans).

So I can right-click the main files and tell it to run and the output is displayed in the log window. That is fine. But I want to be able to extract them out into actual JAR files so that they can be ran individually.

These files also rely on the entire, larger project. Lots of packages, etc.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2025-01-04 18:05:57

这是一种快速但肮脏的 dos 方法。创建一个文件夹并 cd 进入其中。

>md test
>cd test

将 WAR 文件、prepare-tests.batmake-classpath.bat 文件复制到该文件夹​​中。

其中 prepare-tests.bat 是:

md unpacked
cd unpacked
jar xvf ..\%1
cd WEB-INF\classes
jar cvf ..\..\..\classes.jar *
cd ..\..\..
call make-classpath

make-classpath.bat 是:

@echo off
<nul set /p =set CLASSPATH=.;> set-classpath.bat
<nul set /p =classes.jar;>> set-classpath.bat
for /f %%a IN ('dir unpacked\WEB-INF\lib *.jar /s/b') do <nul set /p =%%a;>> set-classpath.bat

基本上,这些步骤将解压 WAR、打包 WAR 的类(如果有)并创建指向这些类以及 WEB-INF/lib 文件夹中所有 JAR 的类路径。

然后,您可以通过以下方式运行测试(从您创建的 test 文件夹中):

>prepare-tests my-war.war
>set-classpath
>java com.foo.bar.MyTest

不过,您最好使用 Maven 和 JUnit 等工具来自动化/简化您的测试。

Here's a quick and dirty dos way. Create a folder and cd into it.

>md test
>cd test

Copy your WAR file, the prepare-tests.bat and make-classpath.bat files into that folder.

Where prepare-tests.bat is:

md unpacked
cd unpacked
jar xvf ..\%1
cd WEB-INF\classes
jar cvf ..\..\..\classes.jar *
cd ..\..\..
call make-classpath

And make-classpath.bat is:

@echo off
<nul set /p =set CLASSPATH=.;> set-classpath.bat
<nul set /p =classes.jar;>> set-classpath.bat
for /f %%a IN ('dir unpacked\WEB-INF\lib *.jar /s/b') do <nul set /p =%%a;>> set-classpath.bat

Basically, these steps will unpack the WAR, jar up the WAR's classes (if any) and create a classpath that points to those classes plus all the JARs in your WEB-INF/lib folder.

You can then run tests (from within the test folder you created) by:

>prepare-tests my-war.war
>set-classpath
>java com.foo.bar.MyTest

You'd be much better off using tools like Maven and JUnit to automate/facilitate your testing though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文